Skip to content

Commit

Permalink
Improve test coverage for updates of packages
Browse files Browse the repository at this point in the history
  • Loading branch information
mortent committed Nov 4, 2020
1 parent 4b6af96 commit 85faa85
Show file tree
Hide file tree
Showing 10 changed files with 774 additions and 231 deletions.
2 changes: 1 addition & 1 deletion e2e/e2eutils.go
Expand Up @@ -34,7 +34,7 @@ func SetupGitRepo(t *testing.T) (*testutil.TestGitRepo, string, func()) {
t.FailNow()
}

g, _, c := testutil.SetupDefaultRepoAndWorkspace(t)
g, _, c := testutil.SetupDefaultRepoAndWorkspace(t, testutil.Dataset1)
upstream := g.RepoDirectory

clean := func() {
Expand Down
4 changes: 2 additions & 2 deletions internal/cmddiff/cmddiff_test.go
Expand Up @@ -45,9 +45,9 @@ func TestCmdInvalidDiffTool(t *testing.T) {
}

func TestCmdExecute(t *testing.T) {
g, dir, clean := testutil.SetupDefaultRepoAndWorkspace(t)
g, w, clean := testutil.SetupDefaultRepoAndWorkspace(t, testutil.Dataset1)
defer clean()
dest := filepath.Join(dir, g.RepoName)
dest := filepath.Join(w.WorkspaceDirectory, g.RepoName)

getRunner := cmdget.NewRunner("")
getRunner.Command.SetArgs([]string{"file://" + g.RepoDirectory + ".git/", "./"})
Expand Down
8 changes: 4 additions & 4 deletions internal/cmdget/cmdget_test.go
Expand Up @@ -32,9 +32,9 @@ import (

// TestCmd_execute tests that get is correctly invoked.
func TestCmd_execute(t *testing.T) {
g, dir, clean := testutil.SetupDefaultRepoAndWorkspace(t)
g, w, clean := testutil.SetupDefaultRepoAndWorkspace(t, testutil.Dataset1)
defer clean()
dest := filepath.Join(dir, g.RepoName)
dest := filepath.Join(w.WorkspaceDirectory, g.RepoName)

r := cmdget.NewRunner("kpt")
r.Command.SetArgs([]string{"file://" + g.RepoDirectory + ".git/", "./"})
Expand Down Expand Up @@ -73,9 +73,9 @@ func TestCmd_execute(t *testing.T) {
// is main and master branch doesn't exist
func TestCmdMainBranch_execute(t *testing.T) {
// set up git repository with master and main branches
g, dir, clean := testutil.SetupDefaultRepoAndWorkspace(t)
g, w, clean := testutil.SetupDefaultRepoAndWorkspace(t, testutil.Dataset1)
defer clean()
dest := filepath.Join(dir, g.RepoName)
dest := filepath.Join(w.WorkspaceDirectory, g.RepoName)
err := g.CheckoutBranch("main", false)
if !assert.NoError(t, err) {
t.FailNow()
Expand Down
2 changes: 1 addition & 1 deletion internal/cmdinit/cmdinit_test.go
Expand Up @@ -141,7 +141,7 @@ func TestCmd_failNotExists(t *testing.T) {

func TestGitUtil_DefaultRef(t *testing.T) {
// set up git repo with both main and master branches
g, _, clean := testutil.SetupDefaultRepoAndWorkspace(t)
g, _, clean := testutil.SetupDefaultRepoAndWorkspace(t, testutil.Dataset1)
defer clean()

// check if master is picked as default if both main and master branches exist
Expand Down
18 changes: 9 additions & 9 deletions internal/cmdupdate/cmdupdate_test.go
Expand Up @@ -32,21 +32,21 @@ import (

// TestCmd_execute verifies that update is correctly invoked.
func TestCmd_execute(t *testing.T) {
g, dir, clean := testutil.SetupDefaultRepoAndWorkspace(t)
g, w, clean := testutil.SetupDefaultRepoAndWorkspace(t, testutil.Dataset1)
defer clean()
dest := filepath.Join(dir, g.RepoName)
dest := filepath.Join(w.WorkspaceDirectory, g.RepoName)

// clone the repo
getCmd := cmdget.NewRunner("kpt")
getCmd.Command.SetArgs([]string{"file://" + g.RepoDirectory + ".git", dir})
getCmd.Command.SetArgs([]string{"file://" + g.RepoDirectory + ".git", w.WorkspaceDirectory})
err := getCmd.Command.Execute()
if !assert.NoError(t, err) {
return
}
if !g.AssertEqual(t, filepath.Join(g.DatasetDirectory, testutil.Dataset1), dest) {
return
}
gitRunner := gitutil.NewLocalGitRunner(dir)
gitRunner := gitutil.NewLocalGitRunner(w.WorkspaceDirectory)
if !assert.NoError(t, gitRunner.Run("add", ".")) {
return
}
Expand All @@ -64,7 +64,7 @@ func TestCmd_execute(t *testing.T) {

// update the cloned package
updateCmd := cmdupdate.NewRunner("kpt")
if !assert.NoError(t, os.Chdir(dir)) {
if !assert.NoError(t, os.Chdir(w.WorkspaceDirectory)) {
return
}
updateCmd.Command.SetArgs([]string{g.RepoName, "--strategy", "fast-forward"})
Expand Down Expand Up @@ -106,13 +106,13 @@ func TestCmd_execute(t *testing.T) {
}

func TestCmd_failUnCommitted(t *testing.T) {
g, dir, clean := testutil.SetupDefaultRepoAndWorkspace(t)
g, w, clean := testutil.SetupDefaultRepoAndWorkspace(t, testutil.Dataset1)
defer clean()
dest := filepath.Join(dir, g.RepoName)
dest := filepath.Join(w.WorkspaceDirectory, g.RepoName)

// clone the repo
getCmd := cmdget.NewRunner("kpt")
getCmd.Command.SetArgs([]string{"file://" + g.RepoDirectory + ".git", dir})
getCmd.Command.SetArgs([]string{"file://" + g.RepoDirectory + ".git", w.WorkspaceDirectory})
err := getCmd.Command.Execute()
if !assert.NoError(t, err) {
return
Expand All @@ -132,7 +132,7 @@ func TestCmd_failUnCommitted(t *testing.T) {

// update the cloned package
updateCmd := cmdupdate.NewRunner("kpt")
if !assert.NoError(t, os.Chdir(dir)) {
if !assert.NoError(t, os.Chdir(w.WorkspaceDirectory)) {
return
}
updateCmd.Command.SetArgs([]string{g.RepoName})
Expand Down
172 changes: 172 additions & 0 deletions internal/testutil/dataset/builder.go
@@ -0,0 +1,172 @@
// Copyright 2020 Google LLC
//
// 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 dataset

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)

var (
deploymentResourceManifest = `
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
`

configMapResourceManifest = `
apiVersion: v1
kind: ConfigMap
metadata:
name: configmap
data:
foo: bar
`
)

var (
DeploymentResource = "deployment"
ConfigMapResource = "configmap"
resources = map[string]resourceInfo{
DeploymentResource: {
filename: "deployment.yaml",
manifest: deploymentResourceManifest,
},
ConfigMapResource: {
filename: "configmap.yaml",
manifest: configMapResourceManifest,
},
}
)

type resourceInfo struct {
filename string
manifest string
}

// Pkg represents a package that can be created on the file system
// by using the Build function
type Pkg struct {
name string

kptfile bool

resources []string

subPkgs []*Pkg
}

// NewPackage creates a new package for testing.
func NewPackage(name string) *Pkg {
return &Pkg{
name: name,
}
}

// WithKptfile configures the current package to have a Kptfile
func (p *Pkg) WithKptfile() *Pkg {
p.kptfile = true
return p
}

// WithResource configures the package to include the provided resource
func (p *Pkg) WithResource(resource string) *Pkg {
p.resources = append(p.resources, resource)
return p
}

// WithSubPackages adds the provided packages as subpackages to the current
// package
func (p *Pkg) WithSubPackages(ps ...*Pkg) *Pkg {
p.subPkgs = append(p.subPkgs, ps...)
return p
}

// Build outputs the current data structure as a set of (nested) package
// in the provided path.
func (p *Pkg) Build(path string) error {
_, err := os.Stat(path)
if err != nil {
return err
}
return buildRecursive(path, p)
}

// Name returns the name of the current package
func (p *Pkg) Name() string {
return p.name
}

func buildRecursive(path string, pkg *Pkg) error {
pkgPath := filepath.Join(path, pkg.name)
err := os.Mkdir(pkgPath, 0700)
if err != nil {
return err
}

if pkg.kptfile {
err := ioutil.WriteFile(filepath.Join(pkgPath, "Kptfile"), []byte(`
apiVersion: krm.dev/v1alpha1
kind: Kptfile
metadata:
name: kpt
`), 0600)
if err != nil {
return err
}
}

for i := range pkg.resources {
r := pkg.resources[i]
info, ok := resources[r]
if !ok {
return fmt.Errorf("unknown resource %s", r)
}
err = ioutil.WriteFile(filepath.Join(pkgPath, info.filename), []byte(info.manifest), 0600)
if err != nil {
return err
}
}

for i := range pkg.subPkgs {
subPkg := pkg.subPkgs[i]
err = buildRecursive(pkgPath, subPkg)
if err != nil {
return err
}
}

return nil
}

0 comments on commit 85faa85

Please sign in to comment.