|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes 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 gcb |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "io/fs" |
| 22 | + "path/filepath" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + |
| 27 | + "k8s.io/release/gcb/gcbfakes" |
| 28 | + "k8s.io/release/pkg/gcp/build" |
| 29 | +) |
| 30 | + |
| 31 | +func TestDirForJobType(t *testing.T) { |
| 32 | + t.Parallel() |
| 33 | + |
| 34 | + const testDir = "testDir" |
| 35 | + errTest := errors.New("test") |
| 36 | + |
| 37 | + for _, tc := range []struct { |
| 38 | + name, jobType string |
| 39 | + prepare func(*gcbfakes.FakeImpl) |
| 40 | + assert func(string, error) |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "success stage", |
| 44 | + jobType: JobTypeStage, |
| 45 | + prepare: func(mock *gcbfakes.FakeImpl) { |
| 46 | + mock.MkdirTempReturns(testDir, nil) |
| 47 | + mock.WriteFileCalls(func(name string, content []byte, mode fs.FileMode) error { |
| 48 | + assert.Contains(t, string(content), "- STAGE") |
| 49 | + assert.Equal(t, filepath.Join(testDir, build.DefaultCloudbuildFile), name) |
| 50 | + return nil |
| 51 | + }) |
| 52 | + }, |
| 53 | + assert: func(dir string, err error) { |
| 54 | + assert.NoError(t, err) |
| 55 | + assert.Equal(t, testDir, dir) |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "success release", |
| 60 | + jobType: JobTypeRelease, |
| 61 | + prepare: func(mock *gcbfakes.FakeImpl) { |
| 62 | + mock.MkdirTempReturns(testDir, nil) |
| 63 | + mock.WriteFileCalls(func(name string, content []byte, mode fs.FileMode) error { |
| 64 | + assert.Contains(t, string(content), "- RELEASE") |
| 65 | + assert.Equal(t, filepath.Join(testDir, build.DefaultCloudbuildFile), name) |
| 66 | + return nil |
| 67 | + }) |
| 68 | + }, |
| 69 | + assert: func(dir string, err error) { |
| 70 | + assert.NoError(t, err) |
| 71 | + assert.Equal(t, testDir, dir) |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "success fast forward", |
| 76 | + jobType: JobTypeFastForward, |
| 77 | + prepare: func(mock *gcbfakes.FakeImpl) { |
| 78 | + mock.MkdirTempReturns(testDir, nil) |
| 79 | + mock.WriteFileCalls(func(name string, content []byte, mode fs.FileMode) error { |
| 80 | + assert.Contains(t, string(content), "- FAST_FORWARD") |
| 81 | + assert.Equal(t, filepath.Join(testDir, build.DefaultCloudbuildFile), name) |
| 82 | + return nil |
| 83 | + }) |
| 84 | + }, |
| 85 | + assert: func(dir string, err error) { |
| 86 | + assert.NoError(t, err) |
| 87 | + assert.Equal(t, testDir, dir) |
| 88 | + }, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "success obs stage", |
| 92 | + jobType: JobTypeObsStage, |
| 93 | + prepare: func(mock *gcbfakes.FakeImpl) { |
| 94 | + mock.MkdirTempReturns(testDir, nil) |
| 95 | + mock.WriteFileCalls(func(name string, content []byte, mode fs.FileMode) error { |
| 96 | + assert.Contains(t, string(content), "- OBS_STAGE") |
| 97 | + assert.Equal(t, filepath.Join(testDir, build.DefaultCloudbuildFile), name) |
| 98 | + return nil |
| 99 | + }) |
| 100 | + }, |
| 101 | + assert: func(dir string, err error) { |
| 102 | + assert.NoError(t, err) |
| 103 | + assert.Equal(t, testDir, dir) |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "success obs release", |
| 108 | + jobType: JobTypeObsRelease, |
| 109 | + prepare: func(mock *gcbfakes.FakeImpl) { |
| 110 | + mock.MkdirTempReturns(testDir, nil) |
| 111 | + mock.WriteFileCalls(func(name string, content []byte, mode fs.FileMode) error { |
| 112 | + assert.Contains(t, string(content), "- OBS_RELEASE") |
| 113 | + assert.Equal(t, filepath.Join(testDir, build.DefaultCloudbuildFile), name) |
| 114 | + return nil |
| 115 | + }) |
| 116 | + }, |
| 117 | + assert: func(dir string, err error) { |
| 118 | + assert.NoError(t, err) |
| 119 | + assert.Equal(t, testDir, dir) |
| 120 | + }, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "failure on temp dir creation", |
| 124 | + jobType: JobTypeStage, |
| 125 | + prepare: func(mock *gcbfakes.FakeImpl) { |
| 126 | + mock.MkdirTempReturns("", errTest) |
| 127 | + }, |
| 128 | + assert: func(dir string, err error) { |
| 129 | + assert.Error(t, err) |
| 130 | + assert.Empty(t, dir) |
| 131 | + }, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "failure on file write", |
| 135 | + jobType: JobTypeStage, |
| 136 | + prepare: func(mock *gcbfakes.FakeImpl) { |
| 137 | + mock.MkdirTempReturns(testDir, nil) |
| 138 | + mock.WriteFileReturns(errTest) |
| 139 | + }, |
| 140 | + assert: func(dir string, err error) { |
| 141 | + assert.Error(t, err) |
| 142 | + assert.Empty(t, dir) |
| 143 | + }, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "failure unknown job type", |
| 147 | + jobType: "wrong", |
| 148 | + prepare: func(mock *gcbfakes.FakeImpl) { |
| 149 | + mock.MkdirTempReturns(testDir, nil) |
| 150 | + }, |
| 151 | + assert: func(dir string, err error) { |
| 152 | + assert.Error(t, err) |
| 153 | + assert.Empty(t, dir) |
| 154 | + }, |
| 155 | + }, |
| 156 | + } { |
| 157 | + tc := tc |
| 158 | + t.Run(tc.name, func(t *testing.T) { |
| 159 | + mock := &gcbfakes.FakeImpl{} |
| 160 | + sut := New() |
| 161 | + sut.impl = mock |
| 162 | + tc.prepare(mock) |
| 163 | + |
| 164 | + dir, err := sut.DirForJobType(tc.jobType) |
| 165 | + tc.assert(dir, err) |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
0 commit comments