-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3472 from saschagrunert/in-memory-jobs
Allow in-memory GCB jobs
- Loading branch information
Showing
5 changed files
with
563 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package gcb | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"path/filepath" | ||
|
||
"k8s.io/release/pkg/gcp/build" | ||
) | ||
|
||
// All available job types | ||
const ( | ||
JobTypeStage = "stage" | ||
JobTypeRelease = "release" | ||
JobTypeFastForward = "fast-forward" | ||
JobTypeObsStage = "obs-stage" | ||
JobTypeObsRelease = "obs-release" | ||
) | ||
|
||
var ( | ||
//go:embed stage/cloudbuild.yaml | ||
stageCloudBuild []byte | ||
|
||
//go:embed release/cloudbuild.yaml | ||
releaseCloudBuild []byte | ||
|
||
//go:embed fast-forward/cloudbuild.yaml | ||
fastForwardCloudBuild []byte | ||
|
||
//go:embed obs-stage/cloudbuild.yaml | ||
obsStageCloudBuild []byte | ||
|
||
//go:embed obs-release/cloudbuild.yaml | ||
obsReleaseCloudBuild []byte | ||
) | ||
|
||
// CloudBuild is the main type of this package. | ||
type CloudBuild struct { | ||
impl | ||
} | ||
|
||
// New creates a new CloudBuild instance. | ||
func New() *CloudBuild { | ||
return &CloudBuild{impl: &defaultImpl{}} | ||
} | ||
|
||
// DirForJobType creates a temp directory containing the default cloudbuild | ||
// file for the provided job type. | ||
func (c *CloudBuild) DirForJobType(jobType string) (string, error) { | ||
tempDir, err := c.impl.MkdirTemp("", "krel-cloudbuild-*") | ||
if err != nil { | ||
return "", fmt.Errorf("create temp cloudbuild dir: %w", err) | ||
} | ||
|
||
var content []byte | ||
switch jobType { | ||
case JobTypeStage: | ||
content = stageCloudBuild | ||
case JobTypeRelease: | ||
content = releaseCloudBuild | ||
case JobTypeFastForward: | ||
content = fastForwardCloudBuild | ||
case JobTypeObsStage: | ||
content = obsStageCloudBuild | ||
case JobTypeObsRelease: | ||
content = obsReleaseCloudBuild | ||
default: | ||
return "", fmt.Errorf("unknown job type: %s", jobType) | ||
} | ||
|
||
if err := c.impl.WriteFile( | ||
filepath.Join(tempDir, build.DefaultCloudbuildFile), | ||
content, | ||
0o600, | ||
); err != nil { | ||
return "", fmt.Errorf("write cloudbuild file: %w", err) | ||
} | ||
|
||
return tempDir, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package gcb | ||
|
||
import ( | ||
"errors" | ||
"io/fs" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"k8s.io/release/gcb/gcbfakes" | ||
"k8s.io/release/pkg/gcp/build" | ||
) | ||
|
||
func TestDirForJobType(t *testing.T) { | ||
t.Parallel() | ||
|
||
const testDir = "testDir" | ||
errTest := errors.New("test") | ||
|
||
for _, tc := range []struct { | ||
name, jobType string | ||
prepare func(*gcbfakes.FakeImpl) | ||
assert func(string, error) | ||
}{ | ||
{ | ||
name: "success stage", | ||
jobType: JobTypeStage, | ||
prepare: func(mock *gcbfakes.FakeImpl) { | ||
mock.MkdirTempReturns(testDir, nil) | ||
mock.WriteFileCalls(func(name string, content []byte, mode fs.FileMode) error { | ||
assert.Contains(t, string(content), "- STAGE") | ||
assert.Equal(t, filepath.Join(testDir, build.DefaultCloudbuildFile), name) | ||
return nil | ||
}) | ||
}, | ||
assert: func(dir string, err error) { | ||
assert.NoError(t, err) | ||
assert.Equal(t, testDir, dir) | ||
}, | ||
}, | ||
{ | ||
name: "success release", | ||
jobType: JobTypeRelease, | ||
prepare: func(mock *gcbfakes.FakeImpl) { | ||
mock.MkdirTempReturns(testDir, nil) | ||
mock.WriteFileCalls(func(name string, content []byte, mode fs.FileMode) error { | ||
assert.Contains(t, string(content), "- RELEASE") | ||
assert.Equal(t, filepath.Join(testDir, build.DefaultCloudbuildFile), name) | ||
return nil | ||
}) | ||
}, | ||
assert: func(dir string, err error) { | ||
assert.NoError(t, err) | ||
assert.Equal(t, testDir, dir) | ||
}, | ||
}, | ||
{ | ||
name: "success fast forward", | ||
jobType: JobTypeFastForward, | ||
prepare: func(mock *gcbfakes.FakeImpl) { | ||
mock.MkdirTempReturns(testDir, nil) | ||
mock.WriteFileCalls(func(name string, content []byte, mode fs.FileMode) error { | ||
assert.Contains(t, string(content), "- FAST_FORWARD") | ||
assert.Equal(t, filepath.Join(testDir, build.DefaultCloudbuildFile), name) | ||
return nil | ||
}) | ||
}, | ||
assert: func(dir string, err error) { | ||
assert.NoError(t, err) | ||
assert.Equal(t, testDir, dir) | ||
}, | ||
}, | ||
{ | ||
name: "success obs stage", | ||
jobType: JobTypeObsStage, | ||
prepare: func(mock *gcbfakes.FakeImpl) { | ||
mock.MkdirTempReturns(testDir, nil) | ||
mock.WriteFileCalls(func(name string, content []byte, mode fs.FileMode) error { | ||
assert.Contains(t, string(content), "- OBS_STAGE") | ||
assert.Equal(t, filepath.Join(testDir, build.DefaultCloudbuildFile), name) | ||
return nil | ||
}) | ||
}, | ||
assert: func(dir string, err error) { | ||
assert.NoError(t, err) | ||
assert.Equal(t, testDir, dir) | ||
}, | ||
}, | ||
{ | ||
name: "success obs release", | ||
jobType: JobTypeObsRelease, | ||
prepare: func(mock *gcbfakes.FakeImpl) { | ||
mock.MkdirTempReturns(testDir, nil) | ||
mock.WriteFileCalls(func(name string, content []byte, mode fs.FileMode) error { | ||
assert.Contains(t, string(content), "- OBS_RELEASE") | ||
assert.Equal(t, filepath.Join(testDir, build.DefaultCloudbuildFile), name) | ||
return nil | ||
}) | ||
}, | ||
assert: func(dir string, err error) { | ||
assert.NoError(t, err) | ||
assert.Equal(t, testDir, dir) | ||
}, | ||
}, | ||
{ | ||
name: "failure on temp dir creation", | ||
jobType: JobTypeStage, | ||
prepare: func(mock *gcbfakes.FakeImpl) { | ||
mock.MkdirTempReturns("", errTest) | ||
}, | ||
assert: func(dir string, err error) { | ||
assert.Error(t, err) | ||
assert.Empty(t, dir) | ||
}, | ||
}, | ||
{ | ||
name: "failure on file write", | ||
jobType: JobTypeStage, | ||
prepare: func(mock *gcbfakes.FakeImpl) { | ||
mock.MkdirTempReturns(testDir, nil) | ||
mock.WriteFileReturns(errTest) | ||
}, | ||
assert: func(dir string, err error) { | ||
assert.Error(t, err) | ||
assert.Empty(t, dir) | ||
}, | ||
}, | ||
{ | ||
name: "failure unknown job type", | ||
jobType: "wrong", | ||
prepare: func(mock *gcbfakes.FakeImpl) { | ||
mock.MkdirTempReturns(testDir, nil) | ||
}, | ||
assert: func(dir string, err error) { | ||
assert.Error(t, err) | ||
assert.Empty(t, dir) | ||
}, | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
mock := &gcbfakes.FakeImpl{} | ||
sut := New() | ||
sut.impl = mock | ||
tc.prepare(mock) | ||
|
||
dir, err := sut.DirForJobType(tc.jobType) | ||
tc.assert(dir, err) | ||
}) | ||
} | ||
} |
Oops, something went wrong.