|
| 1 | +/* |
| 2 | + Copyright © 2026 The CDI Authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package producer |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + cdi "tags.cncf.io/container-device-interface/specs-go" |
| 27 | +) |
| 28 | + |
| 29 | +func TestSave(t *testing.T) { |
| 30 | + testCases := []struct { |
| 31 | + description string |
| 32 | + spec *cdi.Spec |
| 33 | + filename string |
| 34 | + options []Option |
| 35 | + expectedError string |
| 36 | + assert func(*testing.T, string) |
| 37 | + }{ |
| 38 | + { |
| 39 | + description: "empty filename returns directory error", |
| 40 | + filename: "", |
| 41 | + expectedError: "specified path is a directory", |
| 42 | + }, |
| 43 | + { |
| 44 | + description: "spec is written with default permissions", |
| 45 | + spec: &cdi.Spec{ |
| 46 | + Version: "v1.1.0", |
| 47 | + }, |
| 48 | + filename: "test.yaml", |
| 49 | + assert: func(t *testing.T, fullpath string) { |
| 50 | + require.FileExists(t, fullpath) |
| 51 | + info, err := os.Stat(fullpath) |
| 52 | + require.NoError(t, err) |
| 53 | + require.EqualValues(t, os.FileMode(0644), info.Mode().Perm()) |
| 54 | + contents, err := os.ReadFile(fullpath) |
| 55 | + require.NoError(t, err) |
| 56 | + expectedContents := `--- |
| 57 | +cdiVersion: v1.1.0 |
| 58 | +kind: "" |
| 59 | +devices: [] |
| 60 | +` |
| 61 | + require.EqualValues(t, expectedContents, string(contents)) |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + description: "spec is written as json with default permissions", |
| 66 | + spec: &cdi.Spec{ |
| 67 | + Version: "v1.1.0", |
| 68 | + }, |
| 69 | + filename: "test.json", |
| 70 | + assert: func(t *testing.T, fullpath string) { |
| 71 | + require.FileExists(t, fullpath) |
| 72 | + info, err := os.Stat(fullpath) |
| 73 | + require.NoError(t, err) |
| 74 | + require.EqualValues(t, os.FileMode(0644), info.Mode().Perm()) |
| 75 | + contents, err := os.ReadFile(fullpath) |
| 76 | + require.NoError(t, err) |
| 77 | + expectedContents := `{"cdiVersion":"v1.1.0","kind":"","devices":null,"containerEdits":{}}` |
| 78 | + require.EqualValues(t, expectedContents, string(contents)) |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + description: "spec is written with format specified as json", |
| 83 | + spec: &cdi.Spec{ |
| 84 | + Version: "v1.1.0", |
| 85 | + }, |
| 86 | + filename: "test", |
| 87 | + options: []Option{WithOutputFormat("json")}, |
| 88 | + assert: func(t *testing.T, fullpath string) { |
| 89 | + require.FileExists(t, fullpath) |
| 90 | + info, err := os.Stat(fullpath) |
| 91 | + require.NoError(t, err) |
| 92 | + require.EqualValues(t, os.FileMode(0644), info.Mode().Perm()) |
| 93 | + contents, err := os.ReadFile(fullpath) |
| 94 | + require.NoError(t, err) |
| 95 | + expectedContents := `{"cdiVersion":"v1.1.0","kind":"","devices":null,"containerEdits":{}}` |
| 96 | + require.EqualValues(t, expectedContents, string(contents)) |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + description: "spec is written with format specified as yaml", |
| 101 | + spec: &cdi.Spec{ |
| 102 | + Version: "v1.1.0", |
| 103 | + }, |
| 104 | + filename: "test", |
| 105 | + options: []Option{WithOutputFormat("yaml")}, |
| 106 | + assert: func(t *testing.T, fullpath string) { |
| 107 | + require.FileExists(t, fullpath) |
| 108 | + info, err := os.Stat(fullpath) |
| 109 | + require.NoError(t, err) |
| 110 | + require.EqualValues(t, os.FileMode(0644), info.Mode().Perm()) |
| 111 | + contents, err := os.ReadFile(fullpath) |
| 112 | + require.NoError(t, err) |
| 113 | + expectedContents := `--- |
| 114 | +cdiVersion: v1.1.0 |
| 115 | +kind: "" |
| 116 | +devices: [] |
| 117 | +` |
| 118 | + require.EqualValues(t, expectedContents, string(contents)) |
| 119 | + }, |
| 120 | + }, |
| 121 | + { |
| 122 | + description: "spec is written with specified permissions", |
| 123 | + spec: &cdi.Spec{ |
| 124 | + Version: "v1.1.0", |
| 125 | + }, |
| 126 | + filename: "test.yaml", |
| 127 | + options: []Option{ |
| 128 | + WithPermissions(os.FileMode(0666)), |
| 129 | + }, |
| 130 | + assert: func(t *testing.T, fullpath string) { |
| 131 | + require.FileExists(t, fullpath) |
| 132 | + info, err := os.Stat(fullpath) |
| 133 | + require.NoError(t, err) |
| 134 | + require.EqualValues(t, os.FileMode(0666), info.Mode().Perm()) |
| 135 | + contents, err := os.ReadFile(fullpath) |
| 136 | + require.NoError(t, err) |
| 137 | + expectedContents := `--- |
| 138 | +cdiVersion: v1.1.0 |
| 139 | +kind: "" |
| 140 | +devices: [] |
| 141 | +` |
| 142 | + require.EqualValues(t, expectedContents, string(contents)) |
| 143 | + }, |
| 144 | + }, |
| 145 | + { |
| 146 | + description: "validator is called before save", |
| 147 | + spec: &cdi.Spec{ |
| 148 | + Version: "v1.1.0", |
| 149 | + }, |
| 150 | + filename: "test.yaml", |
| 151 | + options: []Option{ |
| 152 | + WithValidator(validatorFunction(func(s *cdi.Spec) error { return fmt.Errorf("invalid spec") })), |
| 153 | + WithPermissions(os.FileMode(0666)), |
| 154 | + }, |
| 155 | + expectedError: "failed to write Spec file: spec validation failed: invalid spec", |
| 156 | + assert: func(t *testing.T, fullpath string) { |
| 157 | + require.NoFileExists(t, fullpath) |
| 158 | + }, |
| 159 | + }, |
| 160 | + } |
| 161 | + |
| 162 | + for _, tc := range testCases { |
| 163 | + t.Run(tc.description, func(t *testing.T) { |
| 164 | + dir := t.TempDir() |
| 165 | + |
| 166 | + fullpath := filepath.Join(dir, tc.filename) |
| 167 | + err := Save(tc.spec, fullpath, tc.options...) |
| 168 | + |
| 169 | + if tc.expectedError == "" { |
| 170 | + require.NoError(t, err) |
| 171 | + } else { |
| 172 | + require.EqualError(t, err, tc.expectedError) |
| 173 | + } |
| 174 | + if tc.assert != nil { |
| 175 | + tc.assert(t, fullpath) |
| 176 | + } |
| 177 | + }) |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments