Skip to content

Commit

Permalink
Enable unit tests in github action (#448)
Browse files Browse the repository at this point in the history
Signed-off-by: zhujian <jiazhu@redhat.com>
  • Loading branch information
zhujian7 committed Aug 8, 2024
1 parent 97027f4 commit 5b2cc49
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ocm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
repository_dispatch:
types: [ocm_changes]
env:
GO_VERSION: '1.21'
GO_VERSION: '1.22'

jobs:
e2e-test:
Expand Down
23 changes: 21 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ jobs:
- name: verify
run: make verify

unit-test:
runs-on: ubuntu-latest
env:
GOPATH: /tmp/go
steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}
- name: Create GOPATH src directory
run: mkdir -p $GOPATH/src/github.com/open-cluster-management
- name: Check out code
uses: actions/checkout@v3
with:
fetch-depth: 1
- name: Copy the code into the GOPATH
run: cp -r . $GOPATH/src/github.com/open-cluster-management/clusteradm
- name: Unit Tests
working-directory: ${{ env.GOPATH }}/src/github.com/open-cluster-management/clusteradm
run: make test

integration-test:
runs-on: ubuntu-latest
steps:
Expand All @@ -38,11 +59,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}

- name: E2E Tests
run: make test-e2e
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ check-copyright:
@build/check-copyright.sh

.PHONY: test
test: deps
test: deps ensure-kubebuilder-tools
@build/run-unit-tests.sh

.PHONY: clean-test
Expand Down
41 changes: 28 additions & 13 deletions pkg/cmd/create/sampleapp/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,32 @@ import (

clusterapiv1 "open-cluster-management.io/api/cluster/v1"
"open-cluster-management.io/clusteradm/pkg/cmd/addon/enable"
installScenario "open-cluster-management.io/clusteradm/pkg/cmd/install/hubaddon/scenario"
hubaddonscenario "open-cluster-management.io/clusteradm/pkg/cmd/install/hubaddon/scenario"
installscenario "open-cluster-management.io/clusteradm/pkg/cmd/install/hubaddon/scenario"
"open-cluster-management.io/clusteradm/pkg/version"
)

// AddonOptions: options used for addon deployment
type AddonOptions struct {
values Values
}

// Values: The values used in the addons deployment template
type Values struct {
hubAddons []string
values hubaddonscenario.Values
}

var _ = ginkgo.Describe("deploy samepleapp to every managed cluster", func() {
var (
ocmVersion = version.GetDefaultBundleVersion()
ocmBundleVersion = version.VersionBundle{}
)

ginkgo.BeforeEach(func() {
if bundleVersion, ok := os.LookupEnv("OCM_BUNDLE_VERSION"); ok && bundleVersion != "" {
ocmVersion = bundleVersion
}

var err error
ocmBundleVersion, err = version.GetVersionBundle(ocmVersion)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
})

var cluster1Name string
var cluster2Name string
var err error
Expand Down Expand Up @@ -120,8 +132,10 @@ var _ = ginkgo.Describe("deploy samepleapp to every managed cluster", func() {
ginkgo.By(fmt.Sprintf("installing %s addon", addon))

ao := AddonOptions{
values: Values{
hubAddons: []string{addon},
values: hubaddonscenario.Values{
HubAddons: []string{addon},
Namespace: addonNamespace,
BundleVersion: ocmBundleVersion,
},
}

Expand All @@ -144,7 +158,7 @@ var _ = ginkgo.Describe("deploy samepleapp to every managed cluster", func() {
files, err := addonPathWalkDir(appDir)
gomega.Expect(err).ToNot(gomega.HaveOccurred(), "install addon error")

err = r.Apply(installScenario.Files, ao.values, files...)
err = r.Apply(installscenario.Files, ao.values, files...)
gomega.Expect(err).ToNot(gomega.HaveOccurred(), "install addon error")

fmt.Fprintf(streams.Out, "Installing built-in %s add-on to namespace %s.\n", addon, addonNamespace)
Expand Down Expand Up @@ -176,9 +190,10 @@ var _ = ginkgo.Describe("deploy samepleapp to every managed cluster", func() {
assertCreatingClusters(cluster2Name)

o := Options{
Streams: streams,
SampleAppName: testSampleAppName,
Namespace: testNamespace,
Streams: streams,
SampleAppName: testSampleAppName,
Namespace: testNamespace,
ClusteradmFlags: clusteradmFlags,
}

clusters := []string{cluster1Name, cluster2Name}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/create/sampleapp/scenario/sampleapp/placement.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright Contributors to the Open Cluster Management project
apiVersion: cluster.open-cluster-management.io/v1alpha1
apiVersion: cluster.open-cluster-management.io/v1beta1
kind: Placement
metadata:
name: {{.SampleAppName}}-placement
Expand Down
27 changes: 16 additions & 11 deletions pkg/cmd/create/sampleapp/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import (
"path/filepath"
"testing"

"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"

apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/envtest"

"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
clusterv1client "open-cluster-management.io/api/client/cluster/clientset/versioned"
"sigs.k8s.io/controller-runtime/pkg/envtest"

genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
)

// nolint:deadcode,varcheck
Expand All @@ -40,6 +40,7 @@ var apiExtensionsClient apiextensionsclient.Interface
var dynamicClient dynamic.Interface
var clusterClient clusterv1client.Interface
var addonClient addonv1alpha1client.Interface
var clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags

func TestIntegrationEnableAddons(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
Expand All @@ -54,8 +55,8 @@ var _ = ginkgo.BeforeSuite(func() {
ErrorIfCRDPathMissing: true,
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "..", "..", "vendor", "open-cluster-management.io", "api", "cluster", "v1"),
filepath.Join("..", "..", "..", "..", "vendor", "open-cluster-management.io", "api", "cluster", "v1alpha1", "0000_03_clusters.open-cluster-management.io_placements.crd.yaml"),
filepath.Join("..", "..", "..", "..", "vendor", "open-cluster-management.io", "api", "cluster", "v1beta1"),
filepath.Join("..", "..", "..", "..", "vendor", "open-cluster-management.io", "api", "cluster", "v1beta2"),
filepath.Join("..", "..", "..", "..", "vendor", "open-cluster-management.io", "api", "addon", "v1alpha1"),
},
}
Expand All @@ -76,6 +77,10 @@ var _ = ginkgo.BeforeSuite(func() {
gomega.Expect(err).NotTo(gomega.HaveOccurred())

restConfig = cfg

// add clusteradm flags
f := cmdutil.NewFactory(TestClientGetter{cfg: cfg})
clusteradmFlags = genericclioptionsclusteradm.NewClusteradmFlags(f)
})

var _ = ginkgo.AfterSuite(func() {
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/join/preflight/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestHubKubeconfigCheck(t *testing.T) {
wantErrorList: []error{errors.New("--hub-apiserver should start with http:// or https://")},
},
{
name: "ca not validated",
name: "no ca",
config: &clientcmdapiv1.Config{
Clusters: []clientcmdapiv1.NamedCluster{
{
Expand All @@ -54,8 +54,8 @@ func TestHubKubeconfigCheck(t *testing.T) {
},
},
},
wantWarnings: nil,
wantErrorList: []error{errors.New("no ca detected, creating hub kubeconfig without ca")},
wantWarnings: []string{"no ca detected, creating hub kubeconfig without ca"},
wantErrorList: nil,
},
}

Expand Down

0 comments on commit 5b2cc49

Please sign in to comment.