Skip to content

Commit 1fe98d2

Browse files
authored
Merge pull request #3472 from saschagrunert/in-memory-jobs
Allow in-memory GCB jobs
2 parents fa97dbf + e0240bf commit 1fe98d2

File tree

5 files changed

+563
-32
lines changed

5 files changed

+563
-32
lines changed

gcb/gcb.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
_ "embed"
21+
"fmt"
22+
"path/filepath"
23+
24+
"k8s.io/release/pkg/gcp/build"
25+
)
26+
27+
// All available job types
28+
const (
29+
JobTypeStage = "stage"
30+
JobTypeRelease = "release"
31+
JobTypeFastForward = "fast-forward"
32+
JobTypeObsStage = "obs-stage"
33+
JobTypeObsRelease = "obs-release"
34+
)
35+
36+
var (
37+
//go:embed stage/cloudbuild.yaml
38+
stageCloudBuild []byte
39+
40+
//go:embed release/cloudbuild.yaml
41+
releaseCloudBuild []byte
42+
43+
//go:embed fast-forward/cloudbuild.yaml
44+
fastForwardCloudBuild []byte
45+
46+
//go:embed obs-stage/cloudbuild.yaml
47+
obsStageCloudBuild []byte
48+
49+
//go:embed obs-release/cloudbuild.yaml
50+
obsReleaseCloudBuild []byte
51+
)
52+
53+
// CloudBuild is the main type of this package.
54+
type CloudBuild struct {
55+
impl
56+
}
57+
58+
// New creates a new CloudBuild instance.
59+
func New() *CloudBuild {
60+
return &CloudBuild{impl: &defaultImpl{}}
61+
}
62+
63+
// DirForJobType creates a temp directory containing the default cloudbuild
64+
// file for the provided job type.
65+
func (c *CloudBuild) DirForJobType(jobType string) (string, error) {
66+
tempDir, err := c.impl.MkdirTemp("", "krel-cloudbuild-*")
67+
if err != nil {
68+
return "", fmt.Errorf("create temp cloudbuild dir: %w", err)
69+
}
70+
71+
var content []byte
72+
switch jobType {
73+
case JobTypeStage:
74+
content = stageCloudBuild
75+
case JobTypeRelease:
76+
content = releaseCloudBuild
77+
case JobTypeFastForward:
78+
content = fastForwardCloudBuild
79+
case JobTypeObsStage:
80+
content = obsStageCloudBuild
81+
case JobTypeObsRelease:
82+
content = obsReleaseCloudBuild
83+
default:
84+
return "", fmt.Errorf("unknown job type: %s", jobType)
85+
}
86+
87+
if err := c.impl.WriteFile(
88+
filepath.Join(tempDir, build.DefaultCloudbuildFile),
89+
content,
90+
0o600,
91+
); err != nil {
92+
return "", fmt.Errorf("write cloudbuild file: %w", err)
93+
}
94+
95+
return tempDir, nil
96+
}

gcb/gcb_test.go

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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

Comments
 (0)