Skip to content

Commit

Permalink
Merge pull request #27 from coreydaley/jira_ship_0004_build_env_vars
Browse files Browse the repository at this point in the history
SHIP-0004: Build Environment Variables
  • Loading branch information
openshift-merge-robot authored Oct 8, 2021
2 parents 44c0a85 + 0fef193 commit 4831482
Show file tree
Hide file tree
Showing 308 changed files with 18,206 additions and 17,770 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@ jobs:

- name: Check out code into the Go module directory
uses: actions/checkout@v2
with:
submodules: recursive

- name: Install Tools
run: sudo apt-get update && sudo apt-get install -y make gcc

- name: Setup BATS
uses: mig4/setup-bats@v1
with:
bats-version: 1.2.1

- name: Install kubectl
uses: azure/setup-kubectl@v1
with:
Expand Down
12 changes: 12 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[submodule "test/e2e/bats/core"]
path = test/e2e/bats/core
url = https://github.com/bats-core/bats-core
[submodule "test/e2e/bats/support"]
path = test/e2e/bats/support
url = https://github.com/bats-core/bats-support
[submodule "test/e2e/bats/assert"]
path = test/e2e/bats/assert
url = https://github.com/bats-core/bats-assert
[submodule "test/e2e/bats/file"]
path = test/e2e/bats/file
url = https://github.com/bats-core/bats-file
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ test: test-unit test-e2e
test-unit:
go test $(GO_FLAGS) $(GO_TEST_FLAGS) $(CMD) $(PKG) $(ARGS)

# runs all end-to-end tests using bats, it assumes bats is installed
# looks for *.bats files in the test/e2e directory and runs them
test-e2e:
./test/e2e/e2e.bats
./test/e2e/bats/core/bin/bats --recursive test/e2e/*.bats

# wait for KinD cluster to be on ready state, so tests can be performed
verify-kind:
Expand Down
17 changes: 16 additions & 1 deletion docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,22 @@ The install script waits for the Controller instance to be running.
[BATS][batsCore] is a testing framework for Bash. It's structured as a regular script with enhanced
syntax to define test cases, collect results, and more.

To run BATS based tests, make sure you have `bats` installed, and then execute:
The BATS core tool and it's supporting libraries are linked in this project as `git submodules`,
alleviating the need to install BATS yourself.

To run BATS based tests, make sure that you have cloned the project (including all submodules):

```sh
# Option 1: Clone the repository and populate submodules right away
git clone --recurse-submodules --depth 1 https://github.com/shipwright-io/cli.git

# Option 2: Populate submodules after cloning
cd /your/project/directory/cli
git submodule init
git submodule update
```

then run:

```sh
make test-e2e
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ go 1.15
require (
github.com/mailru/easyjson v0.7.1 // indirect
github.com/onsi/gomega v1.10.3
github.com/shipwright-io/build v0.5.2-0.20210715083206-5d8fb411a1eb
github.com/spf13/cobra v1.1.3
github.com/shipwright-io/build v0.6.0
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/texttheater/golang-levenshtein/levenshtein v0.0.0-20200805054039-cae8b0eaed6c
k8s.io/api v0.20.2
k8s.io/apimachinery v0.20.2
k8s.io/api v0.20.6
k8s.io/apimachinery v0.20.6
k8s.io/cli-runtime v0.20.2
k8s.io/client-go v0.20.2
k8s.io/client-go v0.20.6
k8s.io/utils v0.0.0-20210629042839-4a2b36d8d73f
)

Expand Down
415 changes: 415 additions & 0 deletions go.sum

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions pkg/shp/cmd/build/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"fmt"

buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/shipwright-io/cli/pkg/shp/cmd/runner"
"github.com/shipwright-io/cli/pkg/shp/flags"
"github.com/shipwright-io/cli/pkg/shp/params"
"github.com/spf13/cobra"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"

"github.com/shipwright-io/cli/pkg/shp/cmd/runner"
"github.com/shipwright-io/cli/pkg/shp/flags"
"github.com/shipwright-io/cli/pkg/shp/params"
"github.com/shipwright-io/cli/pkg/shp/util"
)

// CreateCommand contains data input from user
Expand Down Expand Up @@ -59,6 +61,13 @@ func (c *CreateCommand) Run(params *params.Params, io *genericclioptions.IOStrea
},
Spec: *c.buildSpec,
}

envs, err := c.cmd.Flags().GetStringArray("env")
if err != nil {
return err
}
b.Spec.Env = append(b.Spec.Env, util.StringSliceToEnvVarSlice(envs)...)

flags.SanitizeBuildSpec(&b.Spec)

clientset, err := params.ShipwrightClientSet()
Expand Down
15 changes: 12 additions & 3 deletions pkg/shp/cmd/buildrun/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"fmt"

buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/shipwright-io/cli/pkg/shp/cmd/runner"
"github.com/shipwright-io/cli/pkg/shp/flags"
"github.com/shipwright-io/cli/pkg/shp/params"
"github.com/spf13/cobra"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"

"github.com/shipwright-io/cli/pkg/shp/cmd/runner"
"github.com/shipwright-io/cli/pkg/shp/flags"
"github.com/shipwright-io/cli/pkg/shp/params"
"github.com/shipwright-io/cli/pkg/shp/util"
)

// CreateCommand reprents the build's create subcommand.
Expand Down Expand Up @@ -60,6 +62,13 @@ func (c *CreateCommand) Run(params *params.Params, ioStreams *genericclioptions.
},
Spec: *c.buildRunSpec,
}

envs, err := c.cmd.Flags().GetStringArray("env")
if err != nil {
return err
}
br.Spec.Env = append(br.Spec.Env, util.StringSliceToEnvVarSlice(envs)...)

flags.SanitizeBuildRunSpec(&br.Spec)

clientset, err := params.ShipwrightClientSet()
Expand Down
5 changes: 5 additions & 0 deletions pkg/shp/flags/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package flags
import (
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/spf13/pflag"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
Expand Down Expand Up @@ -37,6 +38,7 @@ func BuildSpecFromFlags(flags *pflag.FlagSet) *buildv1alpha1.BuildSpec {
imageFlags(flags, "builder", spec.Builder)
imageFlags(flags, "output", &spec.Output)
timeoutFlags(flags, spec.Timeout)
envFlags(flags, spec.Env)

return spec
}
Expand All @@ -56,6 +58,9 @@ func SanitizeBuildSpec(b *buildv1alpha1.BuildSpec) {
if b.Builder.Image == "" && b.Builder.Credentials == nil {
b.Builder = nil
}
if len(b.Env) == 0 {
b.Env = nil
}
}
if b.Timeout != nil && b.Timeout.Duration == 0 {
b.Timeout = nil
Expand Down
6 changes: 6 additions & 0 deletions pkg/shp/flags/buildrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ func BuildRunSpecFromFlags(flags *pflag.FlagSet) *buildv1alpha1.BuildRunSpec {
Output: &buildv1alpha1.Image{
Credentials: &corev1.LocalObjectReference{},
},
Env: []corev1.EnvVar{},
}

buildRefFlags(flags, spec.BuildRef)
serviceAccountFlags(flags, spec.ServiceAccount)
timeoutFlags(flags, spec.Timeout)
imageFlags(flags, "output", spec.Output)
envFlags(flags, spec.Env)

return spec
}
Expand All @@ -52,4 +54,8 @@ func SanitizeBuildRunSpec(br *buildv1alpha1.BuildRunSpec) {
if br.Timeout != nil && br.Timeout.Duration == 0 {
br.Timeout = nil
}

if len(br.Env) == 0 {
br.Env = nil
}
}
16 changes: 16 additions & 0 deletions pkg/shp/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/spf13/pflag"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -18,6 +20,8 @@ const (
BuilderCredentialsSecretFlag = "builder-credentials-secret"
// DockerfileFlag command-line flag.
DockerfileFlag = "dockerfile"
// EnvFlag command-line flag.
EnvFlag = "env"
// SourceURLFlag command-line flag.
SourceURLFlag = "source-url"
// SourceRevisionFlag command-line flag.
Expand Down Expand Up @@ -160,3 +164,15 @@ func serviceAccountFlags(flags *pflag.FlagSet, sa *buildv1alpha1.ServiceAccount)
"generate a Kubernetes service-account for the build",
)
}

// envFlags registers flags for adding corev1.EnvVars.
func envFlags(flags *pflag.FlagSet, envs []corev1.EnvVar) {
var e []string
flags.StringArrayVarP(
&e,
"env",
"e",
[]string{},
"specify a key-value pair for an environment variable to set for the build container",
)
}
17 changes: 17 additions & 0 deletions pkg/shp/util/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package util

import (
"strings"

corev1 "k8s.io/api/core/v1"
)

func StringSliceToEnvVarSlice(envs []string) []corev1.EnvVar {
envVars := []corev1.EnvVar{}

for _, e := range envs {
d := strings.Split(e, "=")
envVars = append(envVars, corev1.EnvVar{Name: d[0], Value: d[1]})
}
return envVars
}
54 changes: 54 additions & 0 deletions pkg/shp/util/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package util

import (
"reflect"
"testing"

corev1 "k8s.io/api/core/v1"
)

func TestStringSliceToEnvVar(t *testing.T) {
type args struct {
envs []string
}
tests := []struct {
name string
args args
want []corev1.EnvVar
}{
{
name: "no envs",
args: args{
envs: []string{},
},
want: []corev1.EnvVar{},
},
{
name: "one env",
args: args{
envs: []string{"my-name=my-value"},
},
want: []corev1.EnvVar{
{Name: "my-name", Value: "my-value"},
},
},
{
name: "multiple envs",
args: args{
envs: []string{"name-one=value-one", "name-two=value-two", "name-three=value-three"},
},
want: []corev1.EnvVar{
{Name: "name-one", Value: "value-one"},
{Name: "name-two", Value: "value-two"},
{Name: "name-three", Value: "value-three"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := StringSliceToEnvVarSlice(tt.args.envs); !reflect.DeepEqual(got, tt.want) {
t.Errorf("StringSliceToEnvVar() = %#v, want %#v", got, tt.want)
}
})
}
}
1 change: 1 addition & 0 deletions test/e2e/bats/assert
Submodule assert added at 672ad1
1 change: 1 addition & 0 deletions test/e2e/bats/core
Submodule core added at 90a051
1 change: 1 addition & 0 deletions test/e2e/bats/file
Submodule file added at 17fa55
1 change: 1 addition & 0 deletions test/e2e/bats/support
Submodule support added at d140a6
39 changes: 23 additions & 16 deletions test/e2e/e2e.bats
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,32 @@

source test/e2e/helpers.sh

@test "shp binary can be executed" {
result="$(shp --help)"
[ ! -z "$result" ]
setup() {
load 'bats/support/load'
load 'bats/assert/load'
load 'bats/file/load'
}

teardown() {
run kubectl delete builds.shipwright.io --all
run kubectl delete buildruns.shipwright.io --all
}

@test "shp build create succesfully run with 1 argument" {
build_name=$(random_name)
result="$(shp build create ${build_name} --source-url=url --output-image=image)"
[ ! -z "$result" ]
result=$(kubectl get build.shipwright.io ${build_name})
[ ! -z "$result" ]
@test "shp binary can be executed" {
run shp --help
assert_success
}

@test "shp buildrun create succesfully run with 1 argument" {
build_name=$(random_name)
result="$(shp build create ${build_name} --source-url=url --output-image=image)"
[ ! -z "$result" ]
@test "shp [build/buildrun] create should not error when a name is specified" {
# generate random names for our build and buildrun
build_name=$(random_name)
buildrun_name=$(random_name)
result="$(shp buildrun create ${buildrun_name} --buildref-name={$build_name})"
result=$(kubectl get buildrun.shipwright.io ${buildrun_name})
[ ! -z "$result" ]

# ensure that shp build create does not give an error when a build_name is specified
run shp build create ${build_name} --source-url=url --output-image=image
assert_success

# ensure that shp buildrun create does not give an error when a buildrun_name is specified
run shp buildrun create ${buildrun_name} --buildref-name=${build_name}
assert_success
}
Loading

0 comments on commit 4831482

Please sign in to comment.