This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
basesuites.go
135 lines (119 loc) · 3.55 KB
/
basesuites.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (C) 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package common
import (
"bytes"
"context"
"fmt"
"path"
"strings"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type BaseSuite struct {
suite.Suite
Name string
CreateFlags []string
SkipSetupCreate bool
ClientSet *kubernetes.Clientset
Namespace string
}
func (s *BaseSuite) SetupSuite() {
var err error
if !s.SkipSetupCreate {
logrus.Infof("%s: Setting up builder", s.Name)
args := append(
[]string{
s.Name,
},
s.CreateFlags...,
)
err := RunBuildkit("create", args, RunBuildStreams{})
require.NoError(s.T(), err, "%s: builder create failed", s.Name)
}
s.ClientSet, s.Namespace, err = GetKubeClientset()
require.NoError(s.T(), err, "%s: kube client failed", s.Name)
}
func (s *BaseSuite) TearDownSuite() {
LogBuilderLogs(context.Background(), s.Name, s.Namespace, s.ClientSet)
logrus.Infof("%s: Removing builder", s.Name)
err := RunBuildkit("rm", []string{
s.Name,
}, RunBuildStreams{})
require.NoError(s.T(), err, "%s: builder rm failed", s.Name)
configMapClient := s.ClientSet.CoreV1().ConfigMaps(s.Namespace)
_, err = configMapClient.Get(context.Background(), s.Name, metav1.GetOptions{})
require.Error(s.T(), err, "config map wasn't cleaned up")
require.Contains(s.T(), err.Error(), "not found")
}
func (s *BaseSuite) TestSimpleBuild() {
logrus.Infof("%s: Simple Build", s.Name)
dir, cleanup, err := NewSimpleBuildContext()
defer cleanup()
require.NoError(s.T(), err, "Failed to set up temporary build context")
args := []string{"--progress=plain"}
if s.Name != "buildkit" { // TODO wire up the default name variable
args = append(
args,
"--builder", s.Name,
)
}
imageName := "dummy.acme.com/" + s.Name + "replaceme:latest"
args = append(
args,
"--tag", imageName,
dir,
)
err = RunBuild(args, RunBuildStreams{})
if isRootlessCreate(s.CreateFlags) {
require.Error(s.T(), err)
require.Contains(s.T(), err.Error(), "please specify")
} else {
require.NoError(s.T(), err, "build failed")
}
err = RunSimpleBuildImageAsPod(context.Background(), s.Name+"-testbuiltimage", imageName, s.Namespace, s.ClientSet)
require.NoError(s.T(), err, "failed to start pod with image")
}
func isRootlessCreate(flags []string) bool {
for _, flag := range flags {
if strings.Contains(flag, "rootless") {
return true
}
}
return false
}
func (s *BaseSuite) TestLocalOutputTarBuild() {
logrus.Infof("%s: Local Output Tar Build", s.Name)
dir, cleanup, err := NewSimpleBuildContext()
defer cleanup()
require.NoError(s.T(), err, "Failed to set up temporary build context")
args := []string{"--progress=plain"}
if s.Name != "buildkit" { // TODO wire up the default name variable
args = append(
args,
"--builder", s.Name,
)
}
args = append(
args,
"--tag", s.Name+"replaceme:latest",
fmt.Sprintf("--output=type=tar,dest=%s", path.Join(dir, "out.tar")),
dir,
)
err = RunBuild(args, RunBuildStreams{})
require.NoError(s.T(), err, "build failed")
// TODO - consider inspecting the out.tar for validity...
}
func (s *BaseSuite) TestLs() {
buf := &bytes.Buffer{}
err := RunBuildkit("ls", []string{}, RunBuildStreams{Out: buf})
require.NoError(s.T(), err, "%s: ls failed", s.Name)
lines := buf.String()
require.Contains(s.T(), lines, s.Name)
err = RunBuildkit("ls", []string{"dummy"}, RunBuildStreams{})
require.Error(s.T(), err)
require.Contains(s.T(), err.Error(), "requires exactly")
}