Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ctr-pull base suite extension #114

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions extensions/base/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,32 @@
package base

import (
"github.com/networkservicemesh/gotestmd/pkg/suites/shell"

"github.com/networkservicemesh/integration-tests/extensions/checkout"
"github.com/networkservicemesh/integration-tests/extensions/ctrpull"
)

// Suite is a base suite for generating tests. Contains extensions that can be used for assertion and automation goals.
type Suite struct {
checkout.Suite
// Add other extensions here
shell.Suite
// Add extensions here
checkout checkout.Suite
ctrPull ctrpull.Suite
}

func (s *Suite) SetupSuite() {
s.Repository = "networkservicemesh/deployments-k8s"
s.Version = "0530e54c"
s.Dir = "../" // Note: this should be synced with input parameters in gen.go file
s.Suite.SetupSuite()
// checkout
s.checkout.Repository = "networkservicemesh/deployments-k8s"
s.checkout.Version = "0530e54c"
s.checkout.Dir = "../" // Note: this should be synced with input parameters in gen.go file

s.checkout.SetT(s.T())
s.checkout.SetupSuite()

// CTR pull
s.ctrPull.Dir = "../deployments-k8s" // Note: this should be synced with input parameters in gen.go file

s.ctrPull.SetT(s.T())
s.ctrPull.SetupSuite()
}
38 changes: 38 additions & 0 deletions extensions/ctrpull/ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// 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 ctrpull

import "regexp"

var (
ignoreSRIOV = true

ignoreSRIOVPattern = regexp.MustCompile(".*-sriov")
ignoreVFIOPattern = regexp.MustCompile(".*-vfio")
)

func ignored() (ignoreList []*regexp.Regexp) {
if ignoreSRIOV {
ignoreList = append(ignoreList, ignoreSRIOVPattern, ignoreVFIOPattern)
}
return ignoreList
}

// WithSRIOV enables prefetching SR-IOV test applications
func WithSRIOV() {
ignoreSRIOV = false
}
102 changes: 102 additions & 0 deletions extensions/ctrpull/k8s.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// 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 ctrpull

const createNamespace = `
cat > ctr-pull-namespace.yaml <<EOF
---
apiVersion: v1
kind: Namespace
metadata:
name: ctr-pull
EOF
`

const createConfigMap = `
cat > ctr-pull-configmap.yaml <<EOF
---
apiVersion: v1
kind: ConfigMap
metadata:
name: ctr-pull
data:
ctr-pull.sh: |
#!/bin/sh

for image in {{.TestImages}}; do
if ! ctr -n=k8s.io image ls -q | grep "\${image}"; then
ctr -n=k8s.io image pull "\${image}"
fi
done
EOF
`

const createDaemonSet = `
cat > ctr-pull.yaml <<EOF
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: ctr-pull
labels:
app: ctr-pull
spec:
selector:
matchLabels:
app: ctr-pull
template:
metadata:
labels:
app: ctr-pull
spec:
initContainers:
- name: ctr-pull
image: docker:latest
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "/root/scripts/ctr-pull.sh"]
volumeMounts:
- name: containerd
mountPath: /run/containerd/containerd.sock
- name: scripts
mountPath: /root/scripts
containers:
- name: pause
image: google/pause:latest
volumes:
- name: containerd
hostPath:
path: /run/containerd/containerd.sock
- name: scripts
configMap:
name: ctr-pull
EOF
`

const createKustomization = `
cat > kustomization.yaml <<EOF
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: ctr-pull

resources:
- ctr-pull-namespace.yaml
- ctr-pull-configmap.yaml
- ctr-pull.yaml
EOF
`
150 changes: 150 additions & 0 deletions extensions/ctrpull/suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// 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 ctrpull

import (
"bufio"
"os"
"path/filepath"
"regexp"
"strings"
"sync"

"github.com/google/uuid"
"github.com/stretchr/testify/require"

"github.com/networkservicemesh/gotestmd/pkg/suites/shell"
)

const (
defaultDomain = "docker.io/"
defaultTag = ":latest"
)

// Suite creates `ctr-pull` daemonset which pulls all test images for all cluster nodes.
type Suite struct {
shell.Suite
Dir string
}

var once sync.Once

func (s *Suite) SetupSuite() {
once.Do(func() {
testImages, err := s.findTestImages()
require.NoError(s.T(), err)

tmpDir := uuid.NewString()
require.NoError(s.T(), os.MkdirAll(tmpDir, 0750))

r := s.Runner(tmpDir)

r.Run(createNamespace)
r.Run(strings.ReplaceAll(createConfigMap, "{{.TestImages}}", strings.Join(testImages, " ")))
r.Run(createDaemonSet)
r.Run(createKustomization)

r.Run("kubectl apply -k .")
r.Run("kubectl -n ctr-pull wait --timeout=10m --for=condition=ready pod -l app=ctr-pull")

r.Run("kubectl delete ns ctr-pull")
_ = os.RemoveAll(tmpDir)
})
}

func (s *Suite) findTestImages() ([]string, error) {
imagePattern := regexp.MustCompile(".*image: (?P<image>.*)")
imageSubexpIndex := imagePattern.SubexpIndex("image")

var testImages []string
walkFunc := func(path string, info os.FileInfo, err error) error {
if ok, skipErr := s.shouldSkipWithError(info, err); ok {
return skipErr
}

file, err := os.Open(path)
if err != nil {
return err
}

scanner := bufio.NewScanner(file)
for scanner.Scan() {
if imagePattern.MatchString(scanner.Text()) {
image := imagePattern.FindAllStringSubmatch(scanner.Text(), -1)[0][imageSubexpIndex]
testImages = append(testImages, s.fullImageName(image))
}
}

return nil
}

if err := filepath.Walk(filepath.Join(s.Dir, "apps"), walkFunc); err != nil {
return nil, err
}
if err := filepath.Walk(filepath.Join(s.Dir, "examples", "spire"), walkFunc); err != nil {
return nil, err
}

return testImages, nil
}

func (s *Suite) shouldSkipWithError(info os.FileInfo, err error) (bool, error) {
if err != nil {
return true, err
}

if info.IsDir() {
for _, ignoredPattern := range ignored() {
if ignoredPattern.MatchString(info.Name()) {
return true, filepath.SkipDir
}
}
return true, nil
}

if !strings.HasSuffix(info.Name(), ".yaml") {
return true, nil
}

return false, nil
}

func (s *Suite) fullImageName(image string) string {
// domain/library/name:tag

split := strings.Split(image, "/")
switch len(split) {
case 3:
// nothing to do
case 2:
image = defaultDomain + image
default:
return ""
}

split = strings.Split(image, ":")
switch len(split) {
case 2:
// nothing to do
case 1:
image += defaultTag
default:
return ""
}

return image
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/networkservicemesh/integration-tests
go 1.15

require (
github.com/google/uuid v1.2.0
github.com/magefile/mage v1.11.0 // indirect
github.com/networkservicemesh/gotestmd v0.0.0-20210216193754-cdd0b6126789
github.com/sirupsen/logrus v1.7.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g=
github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/magefile/mage v1.11.0 h1:C/55Ywp9BpgVVclD3lRnSYCwXTYxmSppIgLeDYlNuls=
Expand Down