Skip to content

Commit

Permalink
Merge pull request #182 from zzxwill/e2e-enhance
Browse files Browse the repository at this point in the history
Add more e2e tests
  • Loading branch information
zzxwill authored Nov 21, 2021
2 parents e2911d9 + fad4557 commit 57a3abd
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 13 deletions.
15 changes: 15 additions & 0 deletions controllers/client/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package client

import (
"k8s.io/client-go/kubernetes"
config2 "sigs.k8s.io/controller-runtime/pkg/client/config"
)

// InitClientSet initializes the Go client set
func InitClientSet() (*kubernetes.Clientset, error) {
config, err := config2.GetConfig()
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(config)
}
9 changes: 0 additions & 9 deletions controllers/terraform/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
config2 "sigs.k8s.io/controller-runtime/pkg/client/config"
)

func initClientSet() (*kubernetes.Clientset, error) {
config, err := config2.GetConfig()
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(config)
}

func getPodLog(ctx context.Context, client *kubernetes.Clientset, namespace, jobName string) (string, error) {
label := fmt.Sprintf("job-name=%s", jobName)
pods, err := client.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{LabelSelector: label})
Expand Down
4 changes: 3 additions & 1 deletion controllers/terraform/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (

"github.com/pkg/errors"
"k8s.io/klog/v2"

"github.com/oam-dev/terraform-controller/controllers/client"
)

// GetTerraformStatus will get Terraform execution status
func GetTerraformStatus(ctx context.Context, namespace, jobName string) error {
klog.InfoS("checking Terraform execution status", "Namespace", namespace, "Job", jobName)
clientSet, err := initClientSet()
clientSet, err := client.InitClientSet()
if err != nil {
klog.ErrorS(err, "failed to init clientSet")
return err
Expand Down
124 changes: 124 additions & 0 deletions e2e/configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package e2e

import (
"fmt"
"golang.org/x/net/context"
"gotest.tools/assert"
kerrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"

"github.com/oam-dev/terraform-controller/controllers/client"
)

const backendSecretNamespace = "vela-system"

func TestConfiguration(t *testing.T) {
clientSet, err := client.InitClientSet()
assert.NilError(t, err)
ctx := context.Background()

klog.Info("1. Applying Configuration")
pwd, _ := os.Getwd()
configuration := filepath.Join(pwd, "..", "examples/alibaba/eip/configuration_eip.yaml")
cmd := fmt.Sprintf("kubectl apply -f %s", configuration)
err = exec.Command("bash", "-c", cmd).Start()
assert.NilError(t, err)

klog.Info("2. Checking Configuration status")
for i := 0; i < 60; i++ {
var fields []string
output, err := exec.Command("bash", "-c", "kubectl get configuration").Output()
assert.NilError(t, err)

lines := strings.Split(string(output), "\n")
for i, line := range lines {
if i == 0 {
continue
}
fields = strings.Fields(line)
if len(fields) == 3 && fields[0] == "alibaba-eip" && fields[1] == "Available" {
goto continueCheck
}
}
if i == 59 {
t.Error("Configuration is not ready")
}
time.Sleep(time.Second * 5)
}

continueCheck:
klog.Info("3. Checking Configuration status")

klog.Info("- Checking ConfigMap which stores .tf")
_, err = clientSet.CoreV1().ConfigMaps("default").Get(ctx, "tf-alibaba-eip", v1.GetOptions{})
assert.NilError(t, err)

klog.Info("- Checking Secret which stores Backend")
_, err = clientSet.CoreV1().Secrets(backendSecretNamespace).Get(ctx, "tfstate-default-alibaba-eip", v1.GetOptions{})
assert.NilError(t, err)

klog.Info("- Checking Secret which stores outputs")
_, err = clientSet.CoreV1().Secrets("default").Get(ctx, "eip-conn", v1.GetOptions{})
assert.NilError(t, err)

klog.Info("- Checking Secret which stores variables")
_, err = clientSet.CoreV1().Secrets("default").Get(ctx, "variable-alibaba-eip", v1.GetOptions{})
assert.NilError(t, err)

klog.Info("4. Deleting Configuration")
cmd = fmt.Sprintf("kubectl delete -f %s", configuration)
err = exec.Command("bash", "-c", cmd).Start()
assert.NilError(t, err)

klog.Info("5. Checking Configuration is deleted")
for i := 0; i < 60; i++ {
var (
fields []string
existed bool
)
output, err := exec.Command("bash", "-c", "kubectl get configuration").Output()
assert.NilError(t, err)

lines := strings.Split(string(output), "\n")

for j, line := range lines {
if j == 0 {
continue
}
fields = strings.Fields(line)
if len(fields) == 3 && fields[0] == "alibaba-eip" {
existed = true
}
}
if existed {
if i == 59 {
t.Error("Configuration is not ready")
}

time.Sleep(time.Second * 5)
continue
} else {
break
}
}

klog.Info("6. Checking Secrets and ConfigMap which should all be deleted")
_, err = clientSet.CoreV1().Secrets("default").Get(ctx, "variable-alibaba-eip", v1.GetOptions{})
assert.Equal(t, kerrors.IsNotFound(err), true)

_, err = clientSet.CoreV1().Secrets("default").Get(ctx, "eip-conn", v1.GetOptions{})
assert.Equal(t, kerrors.IsNotFound(err), true)

_, err = clientSet.CoreV1().Secrets(backendSecretNamespace).Get(ctx, "tfstate-default-alibaba-eip", v1.GetOptions{})
assert.Equal(t, kerrors.IsNotFound(err), true)

_, err = clientSet.CoreV1().ConfigMaps("default").Get(ctx, "tf-alibaba-eip", v1.GetOptions{})
assert.Equal(t, kerrors.IsNotFound(err), true)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package e2e
package configurations

import (
"fmt"
Expand All @@ -17,7 +17,6 @@ func TestConfiguration(t *testing.T) {
It("All Configurations should become `Available`", func() {
pwd, _ := os.Getwd()
configurations := []string{
"examples/alibaba/eip/configuration_eip.yaml",
"examples/alibaba/eip/configuration_eip_remote.yaml",
"examples/alibaba/eip/configuration_eip_remote_subdirectory.yaml",
"examples/alibaba/rds/configuration_hcl_rds.yaml",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e
package configurations

import (
"testing"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/onsi/ginkgo v1.16.2
github.com/onsi/gomega v1.12.0
github.com/pkg/errors v0.9.1
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781
google.golang.org/appengine v1.6.5 // indirect
gotest.tools v2.2.0+incompatible
k8s.io/api v0.18.8
Expand Down

0 comments on commit 57a3abd

Please sign in to comment.