Skip to content

Commit

Permalink
make the testjob source dir configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeng committed Mar 26, 2024
1 parent 5e67ca1 commit b0a1b78
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 8 deletions.
51 changes: 44 additions & 7 deletions cmd/ocm-backplane/testJob/createTestJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ Example usage:
"Use this flag to perform a dry run, which will yield the YAML of the job without creating it.",
)

cmd.Flags().StringP(
"source-dir",
"s",
"",
"Optional source dir for the example script",
)

cmd.Flags().StringP(
"base-image-override",
"i",
Expand Down Expand Up @@ -130,6 +137,12 @@ func runCreateTestJob(cmd *cobra.Command, args []string) error {
return err
}

// Source Dir override flag
sourceDirFlag, err := cmd.Flags().GetString("source-dir")
if err != nil {
return err
}

// raw flag
rawFlag, err := cmd.Flags().GetBool("raw")
if err != nil {
Expand Down Expand Up @@ -172,7 +185,12 @@ func runCreateTestJob(cmd *cobra.Command, args []string) error {
return err
}

cj, err := createTestScriptFromFiles(dryRun)
sourceDir := "./"
if sourceDirFlag != "" {
sourceDir = sourceDirFlag + "/"
}

cj, err := createTestScriptFromFiles(sourceDir, dryRun)
if err != nil {
return err
}
Expand Down Expand Up @@ -208,11 +226,27 @@ func runCreateTestJob(cmd *cobra.Command, args []string) error {
return nil
}

func createTestScriptFromFiles(dryRun bool) (*backplaneApi.CreateTestScriptRunJSONRequestBody, error) {
func checkDirectory(dir string) bool {
info, err := os.Stat(dir)
if os.IsNotExist(err) {
return false
}

return info.IsDir()
}

func createTestScriptFromFiles(sourceDir string, dryRun bool) (*backplaneApi.CreateTestScriptRunJSONRequestBody, error) {

if !checkDirectory(sourceDir) {
return nil, fmt.Errorf("the specified source dir does not exist or it is not a directory")
}

metaFile := sourceDir + "metadata.yaml"

// Read the yaml file from cwd
yamlFile, err := os.ReadFile("metadata.yaml")
yamlFile, err := os.ReadFile(metaFile)
if err != nil {
logger.Errorf("Error reading metadata yaml: %v, ensure you are in a script directory", err)
logger.Errorf("Error reading metadata yaml: %v, ensure either you are in a script directory or you have specified the correct source dir", err)
return nil, err
}

Expand All @@ -223,17 +257,20 @@ func createTestScriptFromFiles(dryRun bool) (*backplaneApi.CreateTestScriptRunJS
logger.Errorf("Error reading metadata: %v", err)
return nil, err
}
fileBody, err := os.ReadFile(scriptMeta.File)

scriptFile := sourceDir + scriptMeta.File

fileBody, err := os.ReadFile(scriptFile)

fileBodyStr := string(fileBody)

// if something like bin/bash or bin/sh at start, read
if err != nil {
logger.Errorf("unable to read file %s, make sure this file exists", scriptMeta.File)
logger.Errorf("unable to read file %s, make sure this file exists", scriptFile)
return nil, err
}

fileBodyStr, err = inlineLibrarySourceFiles(fileBodyStr, scriptMeta.File)
fileBodyStr, err = inlineLibrarySourceFiles(fileBodyStr, scriptFile)
if err != nil {
return nil, err
}
Expand Down
48 changes: 48 additions & 0 deletions cmd/ocm-backplane/testJob/createTestJob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/konfig"

cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"

"github.com/openshift/backplane-cli/pkg/backplaneapi"
backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks"
"github.com/openshift/backplane-cli/pkg/client/mocks"
Expand Down Expand Up @@ -69,6 +71,8 @@ var _ = Describe("testJob create command", func() {
trueClusterID string
proxyURI string
tempDir string
sourceDir string
workingDir string

fakeResp *http.Response

Expand All @@ -80,6 +84,8 @@ var _ = Describe("testJob create command", func() {
mockCtrl = gomock.NewController(GinkgoT())
mockClient = mocks.NewMockClientInterface(mockCtrl)

workingDir = konfig.CurrentWorkingDir()

tempDir, _ = os.MkdirTemp("", "createJobTest")

_ = os.WriteFile(path.Join(tempDir, "metadata.yaml"), []byte(MetadataYaml), 0600)
Expand Down Expand Up @@ -126,6 +132,7 @@ var _ = Describe("testJob create command", func() {

Context("create test job", func() {
It("when running with a simple case should work as expected", func() {

mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes()
mockOcmInterface.EXPECT().IsProduction().Return(false, nil)
// It should query for the internal cluster id first
Expand Down Expand Up @@ -175,6 +182,47 @@ var _ = Describe("testJob create command", func() {
Expect(err).To(BeNil())
})

It("should be able to use the specified script dir", func() {

sourceDir, _ = os.MkdirTemp("", "manualScriptDir")
_ = os.WriteFile(path.Join(sourceDir, "metadata.yaml"), []byte(MetadataYaml), 0600)
_ = os.WriteFile(path.Join(sourceDir, "script.sh"), []byte("echo hello"), 0600)
defer os.RemoveAll(sourceDir)

_ = os.Chdir(workingDir)

mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes()
mockOcmInterface.EXPECT().IsProduction().Return(false, nil)
// It should query for the internal cluster id first
mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil)
mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes()
mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes()
mockClientUtil.EXPECT().MakeRawBackplaneAPIClient(proxyURI).Return(mockClient, nil)
mockClient.EXPECT().CreateTestScriptRun(gomock.Any(), trueClusterID, gomock.Any()).Return(fakeResp, nil)

sut.SetArgs([]string{"create", "--cluster-id", testClusterID, "--source-dir", sourceDir})
err := sut.Execute()

Expect(err).To(BeNil())
})

It("should return with correct error message when the given source dir is incorrect", func() {
nonExistDir := "testDir"
mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes()
mockOcmInterface.EXPECT().IsProduction().Return(false, nil)
// It should query for the internal cluster id first
mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil)
mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes()
mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes()
mockClientUtil.EXPECT().MakeRawBackplaneAPIClient(proxyURI).Return(mockClient, nil)

sut.SetArgs([]string{"create", "--cluster-id", testClusterID, "--source-dir", nonExistDir})
err := sut.Execute()

Expect(err).ToNot(BeNil())
Expect(err.Error()).Should(ContainSubstring("does not exist or it is not a directory"))
})

It("Should able use the current logged in cluster if non specified and retrieve from config file", func() {
os.Setenv(info.BackplaneURLEnvName, "https://api-backplane.apps.something.com")
mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
k8s.io/apimachinery v0.28.3
k8s.io/cli-runtime v0.28.3
k8s.io/client-go v0.28.3
sigs.k8s.io/kustomize/api v0.15.0
sigs.k8s.io/yaml v1.4.0
)

Expand Down Expand Up @@ -133,7 +134,6 @@ require (
k8s.io/kube-openapi v0.0.0-20231113174909-778a5567bc1e // indirect
k8s.io/utils v0.0.0-20230505201702-9f6742963106 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.15.0 // indirect
sigs.k8s.io/kustomize/kyaml v0.15.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)

0 comments on commit b0a1b78

Please sign in to comment.