Skip to content

Commit

Permalink
update sanity test failures
Browse files Browse the repository at this point in the history
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
  • Loading branch information
everettraven committed Aug 14, 2023
1 parent 6e08e9a commit d1466de
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 161 deletions.
4 changes: 2 additions & 2 deletions hack/generate/samples/ansible/memcached_molecule.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package ansible

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
Expand Down Expand Up @@ -118,7 +118,7 @@ func ImplementMemcachedMolecule(sample sample.Sample, image string) {
// prevent high load of controller caused by watching all the secrets in the cluster
watchNamespacePatchFileName := "watch_namespace_patch.yaml"
log.Info("adding WATCH_NAMESPACE env patch to watch own namespace")
err = ioutil.WriteFile(filepath.Join(sample.Dir(), "config", "testing", watchNamespacePatchFileName), []byte(watchNamespacePatch), 0644)
err = os.WriteFile(filepath.Join(sample.Dir(), "config", "testing", watchNamespacePatchFileName), []byte(watchNamespacePatch), 0644)
pkg.CheckError("adding watch_namespace_patch.yaml", err)

log.Info("adding WATCH_NAMESPACE env patch to patch list to be applied")
Expand Down
5 changes: 4 additions & 1 deletion pkg/testutils/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ func (gcc *GenericCommandContext) Run(cmd *exec.Cmd, path ...string) ([]byte, er
// make the directory if it does not already exist
if dir != "" {
if _, err := os.Stat(dir); os.IsNotExist(err) {
os.MkdirAll(dir, 0755)
err = os.MkdirAll(dir, 0755)
if err != nil {
return nil, fmt.Errorf("making all directories: %w", err)
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/testutils/e2e/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package e2e

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -38,13 +37,13 @@ func CreateCustomResources(sample sample.Sample, kubectl kubernetes.Kubectl) err
func AllowProjectBeMultiGroup(sample sample.Sample) error {
const multiGroup = `multigroup: true
`
projectBytes, err := ioutil.ReadFile(filepath.Join(sample.Dir(), "PROJECT"))
projectBytes, err := os.ReadFile(filepath.Join(sample.Dir(), "PROJECT"))
if err != nil {
return err
}

projectBytes = append([]byte(multiGroup), projectBytes...)
err = ioutil.WriteFile(filepath.Join(sample.Dir(), "PROJECT"), projectBytes, 0644)
err = os.WriteFile(filepath.Join(sample.Dir(), "PROJECT"), projectBytes, 0644)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/testutils/e2e/olm/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package olm

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -40,7 +40,7 @@ packagemanifests: kustomize %s
$(KUSTOMIZE) build config/manifests | operator-sdk generate packagemanifests -q --version $(VERSION) $(PKG_MAN_OPTS)
`

makefileBytes, err := ioutil.ReadFile(filepath.Join(sample.Dir(), "Makefile"))
makefileBytes, err := os.ReadFile(filepath.Join(sample.Dir(), "Makefile"))
if err != nil {
return err
}
Expand All @@ -54,7 +54,7 @@ packagemanifests: kustomize %s

// update makefile by adding the packagemanifests target
makefileBytes = append([]byte(makefilePackagemanifestsFragment), makefileBytes...)
err = ioutil.WriteFile(filepath.Join(sample.Dir(), "Makefile"), makefileBytes, 0644)
err = os.WriteFile(filepath.Join(sample.Dir(), "Makefile"), makefileBytes, 0644)
if err != nil {
return err
}
Expand Down Expand Up @@ -140,14 +140,14 @@ func removeAllAnnotationLines(annotations map[string]string, filePaths []string)
}

for _, file := range filePaths {
b, err := ioutil.ReadFile(file)
b, err := os.ReadFile(file)
if err != nil {
return err
}
for _, re := range annotationREs {
b = re.ReplaceAll(b, []byte{})
}
err = ioutil.WriteFile(file, b, 0644)
err = os.WriteFile(file, b, 0644)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/testutils/e2e/operator/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func UndeployOperator(sample sample.Sample) error {
cmd := exec.Command("make", "undeploy")
_, err := sample.CommandContext().Run(cmd, sample.Name())
if err != nil {
fmt.Errorf("encountered an error when undeploying the operator: %w", err)
return fmt.Errorf("encountered an error when undeploying the operator: %w", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/testutils/kubernetes/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (kvi *KubeVersionInfo) GitVersion() string {

// KubeVersion is an implementation of the KubernetesVersion interface
type KubeVersion struct {
clientVersion KubeVersionInfo `json:"clientVersion,omitempty"`
serverVersion KubeVersionInfo `json:"serverVersion,omitempty"`
clientVersion KubeVersionInfo
serverVersion KubeVersionInfo
}

// KubeVersionOptions is for configuring a KubeVersion
Expand Down
6 changes: 3 additions & 3 deletions pkg/testutils/sample/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (gs *CliSample) GenerateInit() error {
return fmt.Errorf("encountered an error getting the current working directory: %w", err)
}
// defer returning to the original working directory
defer func() { os.Chdir(cwd) }()
defer func() { _ = os.Chdir(cwd) }()
// Change directory to the context specified
err = os.MkdirAll(gs.Dir(), 0777)
if err != nil {
Expand Down Expand Up @@ -239,7 +239,7 @@ func (gs *CliSample) GenerateApi() error {
return fmt.Errorf("encountered an error getting the current working directory: %w", err)
}
// defer returning to the original working directory
defer func() { os.Chdir(cwd) }()
defer func() { _ = os.Chdir(cwd) }()
// Change directory to the context specified
err = os.MkdirAll(gs.Dir(), os.ModeDir)
if err != nil {
Expand Down Expand Up @@ -288,7 +288,7 @@ func (gs *CliSample) GenerateWebhook() error {
return fmt.Errorf("encountered an error getting the current working directory: %w", err)
}
// defer returning to the original working directory
defer func() { os.Chdir(cwd) }()
defer func() { _ = os.Chdir(cwd) }()
err = os.MkdirAll(gs.Dir(), os.ModeDir)
if err != nil {
return fmt.Errorf("encountered an error creating context directory: %w", err)
Expand Down
145 changes: 1 addition & 144 deletions test/e2e/ansible/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,69 +106,6 @@ var _ = BeforeSuite(func() {
}, 3*time.Minute, time.Second).Should(Succeed())
}

// By("replacing project Dockerfile to use ansible base image with the dev tag")
// err = kbutil.ReplaceRegexInFile(filepath.Join(ansibleSample.Dir(), "Dockerfile"), "quay.io/operator-framework/ansible-operator:.*", "quay.io/operator-framework/ansible-operator:dev")
// Expect(err).Should(Succeed())

// TODO(everettraven): IMO this should be moved to the logic for implementing the sample. Keeping as is for now to make finish the PoC easier
// ---------------------------------------------------

// gvk := ansibleSample.GVKs()[0]

// By("adding Memcached mock task to the role")
// err = kbutil.InsertCode(filepath.Join(ansibleSample.Dir(), "roles", strings.ToLower(gvk.Kind), "tasks", "main.yml"),
// "periodSeconds: 3", memcachedWithBlackListTask)
// Expect(err).NotTo(HaveOccurred())

// By("creating an API definition to add a task to delete the config map")
// cmd := exec.Command(ansibleSample.Binary(), "create", "api",
// "--group", gvk.Group,
// "--version", gvk.Version,
// "--kind", "Memfin",
// "--generate-role")
// _, err = ansibleSample.CommandContext().Run(cmd, ansibleSample.Name())
// Expect(err).NotTo(HaveOccurred())

// By("updating spec of Memfin sample")
// err = kbutil.ReplaceInFile(
// filepath.Join(ansibleSample.Dir(), "config", "samples", fmt.Sprintf("%s_%s_memfin.yaml", gvk.Group, gvk.Version)),
// "# TODO(user): Add fields here",
// "foo: bar")
// Expect(err).NotTo(HaveOccurred())

// By("adding task to delete config map")
// err = kbutil.ReplaceInFile(filepath.Join(ansibleSample.Dir(), "roles", "memfin", "tasks", "main.yml"),
// "# tasks file for Memfin", taskToDeleteConfigMap)
// Expect(err).NotTo(HaveOccurred())

// By("adding to watches finalizer and blacklist")
// err = kbutil.ReplaceInFile(filepath.Join(ansibleSample.Dir(), "watches.yaml"),
// "playbook: playbooks/memcached.yml", memcachedWatchCustomizations)
// Expect(err).NotTo(HaveOccurred())

// By("create API to test watching multiple GVKs")
// cmd = exec.Command(ansibleSample.Binary(), "create", "api",
// "--group", gvk.Group,
// "--version", gvk.Version,
// "--kind", "Foo",
// "--generate-role")
// _, err = ansibleSample.CommandContext().Run(cmd, ansibleSample.Name())
// Expect(err).NotTo(HaveOccurred())

// By("updating spec of Foo sample")
// err = kbutil.ReplaceInFile(
// filepath.Join(ansibleSample.Dir(), "config", "samples", fmt.Sprintf("%s_%s_foo.yaml", gvk.Group, gvk.Version)),
// "# TODO(user): Add fields here",
// "foo: bar")
// Expect(err).NotTo(HaveOccurred())

// By("adding RBAC permissions for the Memcached Kind")
// err = kbutil.ReplaceInFile(filepath.Join(ansibleSample.Dir(), "config", "rbac", "role.yaml"),
// "#+kubebuilder:scaffold:rules", rolesForBaseOperator)
// Expect(err).NotTo(HaveOccurred())

// ---------------------------------------------------

By("building the project image")
err = operator.BuildOperatorImage(ansibleSample, image)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -188,7 +125,7 @@ var _ = AfterSuite(func() {
By("uninstalling prerequisites")
if isPrometheusManagedBySuite {
By("uninstalling Prometheus")
prometheus.UninstallPrometheusOperator(kctl)
Expect(prometheus.UninstallPrometheusOperator(kctl)).To(Succeed())
}

By("destroying container image and work dir")
Expand All @@ -200,83 +137,3 @@ var _ = AfterSuite(func() {
Expect(err).To(BeNil())
}
})

const memcachedWithBlackListTask = `
- operator_sdk.util.k8s_status:
api_version: cache.example.com/v1alpha1
kind: Memcached
name: "{{ ansible_operator_meta.name }}"
namespace: "{{ ansible_operator_meta.namespace }}"
status:
test: "hello world"
- kubernetes.core.k8s:
definition:
kind: Secret
apiVersion: v1
metadata:
name: test-secret
namespace: "{{ ansible_operator_meta.namespace }}"
data:
test: aGVsbG8K
- name: Get cluster api_groups
set_fact:
api_groups: "{{ lookup('kubernetes.core.k8s', cluster_info='api_groups', kubeconfig=lookup('env', 'K8S_AUTH_KUBECONFIG')) }}"
- name: create project if projects are available
kubernetes.core.k8s:
definition:
apiVersion: project.openshift.io/v1
kind: Project
metadata:
name: testing-foo
when: "'project.openshift.io' in api_groups"
- name: Create ConfigMap to test blacklisted watches
kubernetes.core.k8s:
definition:
kind: ConfigMap
apiVersion: v1
metadata:
name: test-blacklist-watches
namespace: "{{ ansible_operator_meta.namespace }}"
data:
arbitrary: afdasdfsajsafj
state: present`

const taskToDeleteConfigMap = `- name: delete configmap for test
kubernetes.core.k8s:
kind: ConfigMap
api_version: v1
name: deleteme
namespace: default
state: absent`

const memcachedWatchCustomizations = `playbook: playbooks/memcached.yml
finalizer:
name: cache.example.com/finalizer
role: memfin
blacklist:
- group: ""
version: v1
kind: ConfigMap`

const rolesForBaseOperator = `
##
## Apply customize roles for base operator
##
- apiGroups:
- ""
resources:
- configmaps
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
#+kubebuilder:scaffold:rules
`

0 comments on commit d1466de

Please sign in to comment.