Skip to content

Commit

Permalink
Merge pull request #361 from saschagrunert/osc-wait-api
Browse files Browse the repository at this point in the history
Add `osc` API for `Version` and `WaitResults`
  • Loading branch information
k8s-ci-robot authored Jul 1, 2024
2 parents 6038b3b + 94fb423 commit b4cc586
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
40 changes: 40 additions & 0 deletions osc/osc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package osc
import (
"fmt"

"github.com/blang/semver/v4"

"sigs.k8s.io/release-utils/command"
)

Expand Down Expand Up @@ -61,3 +63,41 @@ func Output(workDir string, args ...string) (string, error) {
func Status(workDir string, args ...string) (*command.Status, error) {
return command.NewWithWorkDir(workDir, OSCExecutable, args...).Run()
}

// WaitResults waits for the build results. If can fail on error if an osc
// version >= 1.8.0 is being used.
func WaitResults(project, packageName string) error {
ver, err := Version()
if err != nil {
return fmt.Errorf("retrieve version: %w", err)
}

args := []string{
"results",
"-w",
fmt.Sprintf("%s/%s", project, packageName),
}

// Version 1.8.0 contains the feature to fail on wait error
// ref: https://github.com/openSUSE/osc/pull/1573
if ver.GE(semver.Version{Major: 1, Minor: 8}) {
args = append(args, "-F")
}

return command.New(OSCExecutable, args...).RunSuccess()
}

// Version returns the semver version of the osc executable.
func Version() (*semver.Version, error) {
out, err := command.New(OSCExecutable, "version").RunSilentSuccessOutput()
if err != nil {
return nil, fmt.Errorf("run version command: %w", err)
}

ver, err := semver.Parse(out.OutputTrimNL())
if err != nil {
return nil, fmt.Errorf("parse semver version: %w", err)
}

return &ver, nil
}
76 changes: 76 additions & 0 deletions osc/osc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
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 osc_test

import (
"os"
"path/filepath"
"testing"

"github.com/blang/semver/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/release-sdk/osc"
)

const (
pathKey = "PATH"
testProject = "project"
testPackage = "package"
)

func TestWaitResults(t *testing.T) {
for _, version := range []string{"1.6.2", "1.8.0"} {
// Setup a custom OSC executable
tempDir, err := os.MkdirTemp("", "osc-version-test-")
require.NoError(t, err)
require.NoError(t, os.WriteFile(
filepath.Join(tempDir, osc.OSCExecutable),
[]byte(`#!/usr/bin/env sh
if [ "$1" = version ]; then
echo -n `+version+`
else
echo -n "$@" > $(dirname "$(realpath $0)")/res
fi
`),
0o755))

// Change $PATH
pathEnv := os.Getenv(pathKey)
t.Setenv(pathKey, tempDir+":"+pathEnv)

// Run the version test
ver, err := osc.Version()
assert.NoError(t, err)
assert.True(t, ver.EQ(semver.MustParse(version)))

// Run the wait results test
err = osc.WaitResults(testProject, testPackage)
assert.NoError(t, err)
res, err := os.ReadFile(filepath.Join(tempDir, "res"))
assert.NoError(t, err)
testString := "results -w project/package"
if version == "1.8.0" {
testString += " -F"
}
assert.Equal(t, testString, string(res))

// Cleanup
require.Nil(t, os.RemoveAll(tempDir))
}
}

0 comments on commit b4cc586

Please sign in to comment.