|
| 1 | +// Copyright (c) 2023-2026, Nubificus LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package urunce2etesting |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | +) |
| 26 | + |
| 27 | +func TestLoadTestCasesFromJSON(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + content string |
| 31 | + wantErr bool |
| 32 | + wantLen int |
| 33 | + checkCase func(t *testing.T, got []containerTestArgs) |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "valid single case", |
| 37 | + content: `[{"image":"img:latest","name":"tc","expectOut":"hello","seccomp":true}]`, |
| 38 | + wantLen: 1, |
| 39 | + checkCase: func(t *testing.T, got []containerTestArgs) { |
| 40 | + assert.Equal(t, "img:latest", got[0].Image) |
| 41 | + assert.Equal(t, "tc", got[0].Name) |
| 42 | + assert.Equal(t, "hello", got[0].ExpectOut) |
| 43 | + assert.True(t, got[0].Seccomp) |
| 44 | + assert.Nil(t, got[0].TestFunc) |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "empty array returns empty slice", |
| 49 | + content: `[]`, |
| 50 | + wantLen: 0, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "nil slices become empty slices", |
| 54 | + content: `[{"image":"i","name":"n","expectOut":""}]`, |
| 55 | + wantLen: 1, |
| 56 | + checkCase: func(t *testing.T, got []containerTestArgs) { |
| 57 | + assert.Equal(t, []int64{}, got[0].Groups) |
| 58 | + assert.Equal(t, []containerVolume{}, got[0].Volumes) |
| 59 | + assert.Equal(t, []string{}, got[0].SideContainers) |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "volumes are decoded correctly", |
| 64 | + content: func() string { |
| 65 | + cases := []jsonTestCase{{ |
| 66 | + Image: "i", |
| 67 | + Name: "n", |
| 68 | + ExpectOut: "", |
| 69 | + Volumes: []containerVolume{{Source: "/src", Dest: "/dst"}}, |
| 70 | + }} |
| 71 | + b, _ := json.Marshal(cases) |
| 72 | + return string(b) |
| 73 | + }(), |
| 74 | + wantLen: 1, |
| 75 | + checkCase: func(t *testing.T, got []containerTestArgs) { |
| 76 | + require.Len(t, got[0].Volumes, 1) |
| 77 | + assert.Equal(t, "/src", got[0].Volumes[0].Source) |
| 78 | + assert.Equal(t, "/dst", got[0].Volumes[0].Dest) |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "file not found returns error", |
| 83 | + content: "", // signal to use a non-existent path |
| 84 | + wantErr: true, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "invalid JSON returns error", |
| 88 | + content: `{not valid json`, |
| 89 | + wantErr: true, |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + for _, tt := range tests { |
| 94 | + t.Run(tt.name, func(t *testing.T) { |
| 95 | + t.Parallel() |
| 96 | + |
| 97 | + var path string |
| 98 | + if tt.wantErr && tt.content == "" { |
| 99 | + path = filepath.Join(t.TempDir(), "nonexistent.json") |
| 100 | + } else { |
| 101 | + path = filepath.Join(t.TempDir(), "cases.json") |
| 102 | + require.NoError(t, os.WriteFile(path, []byte(tt.content), 0o600)) |
| 103 | + } |
| 104 | + |
| 105 | + got, err := loadTestCasesFromJSON(path) |
| 106 | + if tt.wantErr { |
| 107 | + assert.Error(t, err) |
| 108 | + return |
| 109 | + } |
| 110 | + require.NoError(t, err) |
| 111 | + assert.Len(t, got, tt.wantLen) |
| 112 | + if tt.checkCase != nil { |
| 113 | + tt.checkCase(t, got) |
| 114 | + } |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func TestOptionalJSONTestCases(t *testing.T) { |
| 120 | + tests := []struct { |
| 121 | + name string |
| 122 | + setup func(t *testing.T) string |
| 123 | + wantLen int |
| 124 | + }{ |
| 125 | + { |
| 126 | + name: "absent file returns empty slice", |
| 127 | + setup: func(t *testing.T) string { |
| 128 | + return filepath.Join(t.TempDir(), "missing.json") |
| 129 | + }, |
| 130 | + wantLen: 0, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "valid file returns cases", |
| 134 | + setup: func(t *testing.T) string { |
| 135 | + cases := []jsonTestCase{{Image: "img:latest", Name: "tc", ExpectOut: "ok"}} |
| 136 | + data, _ := json.Marshal(cases) |
| 137 | + path := filepath.Join(t.TempDir(), "cases.json") |
| 138 | + require.NoError(t, os.WriteFile(path, data, 0o600)) |
| 139 | + return path |
| 140 | + }, |
| 141 | + wantLen: 1, |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "invalid JSON returns empty slice", |
| 145 | + setup: func(t *testing.T) string { |
| 146 | + path := filepath.Join(t.TempDir(), "cases.json") |
| 147 | + require.NoError(t, os.WriteFile(path, []byte("{bad json"), 0o600)) |
| 148 | + return path |
| 149 | + }, |
| 150 | + wantLen: 0, |
| 151 | + }, |
| 152 | + } |
| 153 | + |
| 154 | + for _, tt := range tests { |
| 155 | + t.Run(tt.name, func(t *testing.T) { |
| 156 | + t.Parallel() |
| 157 | + path := tt.setup(t) |
| 158 | + got := optionalJSONTestCases(path) |
| 159 | + assert.Len(t, got, tt.wantLen) |
| 160 | + }) |
| 161 | + } |
| 162 | +} |
0 commit comments