Browse Source

Basic integration tests

Run manually with `make integration-test` to test a straightforward
initial sync followed by an automated release using a `flux-test`
minikube profile.
Adam Harrison 2 years ago
parent
commit
f8f5cfe971
5 changed files with 258 additions and 1 deletions
  1. 1
    0
      .gitignore
  2. 4
    1
      Makefile
  3. 130
    0
      test/bin/test-flux
  4. 98
    0
      test/flux-deploy-all.yaml
  5. 25
    0
      test/helloworld-deployment.yaml

+ 1
- 0
.gitignore View File

@@ -37,3 +37,4 @@ testdata/helloworld/.helloworld
37 37
 testdata/sidecar/sidecar-linux-amd64
38 38
 testdata/sidecar/.sidecar
39 39
 docker/fluxy-dumbconf.priv
40
+test/profiles

+ 4
- 1
Makefile View File

@@ -1,5 +1,5 @@
1 1
 .DEFAULT: all
2
-.PHONY: all release-bins clean realclean test
2
+.PHONY: all release-bins clean realclean test integration-test
3 3
 
4 4
 DOCKER?=docker
5 5
 TEST_FLAGS?=
@@ -64,3 +64,6 @@ $(GOPATH)/bin/fluxctl: ./cmd/fluxctl/*.go
64 64
 $(GOPATH)/bin/fluxd: $(FLUXD_DEPS)
65 65
 $(GOPATH)/bin/fluxd: cmd/fluxd/*.go
66 66
 	go install ./cmd/fluxd
67
+
68
+integration-test: all
69
+	test/bin/test-flux

+ 130
- 0
test/bin/test-flux View File

@@ -0,0 +1,130 @@
1
+#!/bin/bash
2
+
3
+set -e
4
+set -o pipefail
5
+
6
+BASEDIR="$(readlink -f "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd )/..")"
7
+PROFILE="flux-test"
8
+PROFILE_DIR="$BASEDIR/profiles/$PROFILE"
9
+KNOWN_HOSTS="$PROFILE_DIR/.known_hosts"
10
+CONFIG_REPO="$PROFILE_DIR/config"
11
+SSH_PRIVATE_KEY="$HOME/.minikube/machines/$PROFILE/id_rsa"
12
+
13
+#############################################################################
14
+# Setup
15
+#############################################################################
16
+
17
+# Clear profile temp dir and reset minikube profile
18
+rm -rf "$PROFILE_DIR"
19
+mkdir -p "$PROFILE_DIR"
20
+minikube delete --profile "$PROFILE" || true
21
+minikube start --profile "$PROFILE"
22
+MINIKUBE_IP=$(minikube --profile "$PROFILE" ip)
23
+
24
+# Copy the latest flux image into the minikube VM
25
+docker save quay.io/weaveworks/flux:latest | (eval $(minikube --profile "$PROFILE" docker-env) && docker load)
26
+
27
+# Create a central git repo inside the minikube VM and get the host key for ssh access
28
+minikube --profile "$PROFILE" ssh -- git init --bare /home/docker/flux.git
29
+ssh-keyscan $MINIKUBE_IP > "$KNOWN_HOSTS"
30
+
31
+# Deploy flux in kube-system namespace, supplying it with the SSH keys it needs to access & authenticate the config repo
32
+kubectl --context "$PROFILE" -n kube-system create secret generic flux-git-deploy --from-file=identity="$SSH_PRIVATE_KEY"
33
+kubectl --context "$PROFILE" -n kube-system create configmap ssh-known-hosts --from-file=known_hosts="$KNOWN_HOSTS"
34
+sed -e "s/MINIKUBE_IP/$MINIKUBE_IP/" "$BASEDIR/flux-deploy-all.yaml" | kubectl --context "$PROFILE" -n kube-system apply -f-
35
+
36
+# Create a git repo in the local filesystem configured with the central git repo in minikube as origin
37
+git init "$CONFIG_REPO"
38
+git -C "$CONFIG_REPO" remote add origin ssh://docker@$MINIKUBE_IP/home/docker/flux.git
39
+export GIT_SSH_COMMAND="ssh -i $SSH_PRIVATE_KEY -o UserKnownHostsFile=$KNOWN_HOSTS"
40
+
41
+#############################################################################
42
+# Test Helpers
43
+#############################################################################
44
+
45
+fail() {
46
+    echo
47
+    echo "*** FAILED ***"
48
+    echo
49
+    exit 1
50
+}
51
+
52
+success() {
53
+    echo
54
+    echo "*** SUCCESS ***"
55
+    echo
56
+    exit 0
57
+}
58
+
59
+wait_for_sync() {
60
+    echo -n "Waiting for sync"
61
+    HEAD_REV=$(git -C "$CONFIG_REPO" rev-list -n 1 HEAD)
62
+    for i in $(seq 1 $1); do
63
+        git -C "$CONFIG_REPO" fetch --tags > /dev/null 2>&1
64
+        FLUX_SYNC_REV=$(git -C "$CONFIG_REPO" rev-list -n 1 flux-sync 2>/dev/null || true)
65
+        if [ "$FLUX_SYNC_REV" = "$HEAD_REV" ]; then
66
+            echo
67
+            return
68
+        fi
69
+        sleep 1
70
+        echo -n .
71
+    done
72
+    fail
73
+}
74
+
75
+wait_for_upstream_commits() {
76
+    echo -n "Waiting for $2 upstream commits"
77
+    for i in $(seq 1 $1); do
78
+        git -C "$CONFIG_REPO" fetch --tags > /dev/null 2>&1
79
+        if [ "$(git -C "$CONFIG_REPO" rev-list --count HEAD..flux-sync)" -eq "2" ]; then
80
+            echo
81
+            return
82
+        fi
83
+        sleep 1
84
+        echo -n .
85
+    done
86
+    fail
87
+}
88
+
89
+services() {
90
+    curl -s "http://$MINIKUBE_IP:30080/api/flux/v6/services?namespace=$1"
91
+}
92
+
93
+controller_container() {
94
+    jq --raw-output ".[]|select(.ID==\"$1\")|.Containers[]|select(.Name==\"$2\")|.Current.ID"
95
+}
96
+
97
+assert_controller_container() {
98
+    if [ "$(services $1|controller_container $2 $3)" != "$4" ]; then
99
+        fail
100
+    fi
101
+}
102
+
103
+#############################################################################
104
+# Tests
105
+#############################################################################
106
+
107
+# Add the helloworld deployment to the config repo and push to the central config repo in minikube
108
+cp "$BASEDIR/helloworld-deployment.yaml" "$CONFIG_REPO"
109
+git -C "$CONFIG_REPO" add helloworld-deployment.yaml
110
+git -C "$CONFIG_REPO" commit -m 'Deploy helloworld'
111
+git -C "$CONFIG_REPO" push
112
+
113
+# Wait two minutes for flux-sync tag to point to local HEAD
114
+wait_for_sync 120
115
+
116
+# Examine services via flux API an assert container versions are correct
117
+assert_controller_container default default:deployment/helloworld helloworld quay.io/weaveworks/helloworld:master-a000001
118
+assert_controller_container default default:deployment/helloworld sidecar quay.io/weaveworks/sidecar:master-a000001
119
+
120
+# Now enable automation
121
+fluxctl --url http://$(minikube -p flux-test ip):30080/api/flux automate --controller=default:deployment/helloworld
122
+
123
+# Wait two minutes for two (automation and release) commits to appear
124
+wait_for_upstream_commits 120 2
125
+
126
+# Examine services via flux API an assert container versions are updated
127
+assert_controller_container default default:deployment/helloworld helloworld quay.io/weaveworks/helloworld:master-9a16ff945b9e
128
+assert_controller_container default default:deployment/helloworld sidecar quay.io/weaveworks/sidecar:master-a000002
129
+
130
+success

+ 98
- 0
test/flux-deploy-all.yaml View File

@@ -0,0 +1,98 @@
1
+---
2
+apiVersion: extensions/v1beta1
3
+kind: Deployment
4
+metadata:
5
+  name: memcached
6
+spec:
7
+  replicas: 1
8
+  template:
9
+    metadata:
10
+      labels:
11
+        name: memcached
12
+    spec:
13
+      containers:
14
+      - name: memcached
15
+        image: memcached:1.4.25
16
+        imagePullPolicy: IfNotPresent
17
+        args:
18
+        - -m 64    # Maximum memory to use, in megabytes. 64MB is default.
19
+        - -p 11211    # Default port, but being explicit is nice.
20
+        - -vv    # This gets us to the level of request logs.
21
+        ports:
22
+        - name: clients
23
+          containerPort: 11211
24
+---
25
+apiVersion: v1
26
+kind: Service
27
+metadata:
28
+  name: memcached
29
+spec:
30
+  # The memcache client uses DNS to get a list of memcached servers and then
31
+  # uses a consistent hash of the key to determine which server to pick.
32
+  clusterIP: None
33
+  ports:
34
+    - name: memcached
35
+      port: 11211
36
+  selector:
37
+    name: memcached
38
+---
39
+# Expose flux to fluxctl
40
+apiVersion: v1
41
+kind: Service
42
+metadata:
43
+  name: flux
44
+spec:
45
+  type: NodePort
46
+  ports:
47
+    - port: 80
48
+      targetPort: 3030
49
+      nodePort: 30080 # Hardwired for test harness access
50
+  selector:
51
+    name: flux
52
+---
53
+apiVersion: extensions/v1beta1
54
+kind: Deployment
55
+metadata:
56
+  name: flux
57
+spec:
58
+  replicas: 1
59
+  strategy:
60
+    type: Recreate
61
+  template:
62
+    metadata:
63
+      labels:
64
+        name: flux
65
+    spec:
66
+      volumes:
67
+      - name: git-key
68
+        secret:
69
+          secretName: flux-git-deploy
70
+      - name: ssh-known-hosts
71
+        configMap:
72
+          name: ssh-known-hosts
73
+          items:
74
+          - key: known_hosts
75
+            path: known_hosts
76
+      containers:
77
+      - name: flux
78
+        # Require locally built image
79
+        image: quay.io/weaveworks/flux:latest
80
+        imagePullPolicy: Never
81
+        ports:
82
+        - containerPort: 3030 # informational
83
+        volumeMounts:
84
+        - name: git-key
85
+          mountPath: /etc/fluxd/ssh
86
+        - name: ssh-known-hosts
87
+          mountPath: /root/.ssh
88
+        args:
89
+        - --memcached-hostname=memcached
90
+        - --memcached-timeout=100ms
91
+        - --memcached-service=memcached
92
+        - --registry-cache-expiry=20m
93
+        # Access minikube hosted config repo by ssh
94
+        - --git-url=ssh://docker@MINIKUBE_IP:/home/docker/flux.git
95
+        - --git-branch=master
96
+        # Tune up to make tests run quicker
97
+        - --registry-poll-interval=60s
98
+        - --git-poll-interval=60s

+ 25
- 0
test/helloworld-deployment.yaml View File

@@ -0,0 +1,25 @@
1
+apiVersion: extensions/v1beta1
2
+kind: Deployment
3
+metadata:
4
+  name: helloworld
5
+spec:
6
+  minReadySeconds: 5
7
+  replicas: 2
8
+  template:
9
+    metadata:
10
+      labels:
11
+        name: helloworld
12
+    spec:
13
+      containers:
14
+      - name: helloworld
15
+        image: quay.io/weaveworks/helloworld:master-a000001
16
+        args:
17
+        - -msg=Ahoy
18
+        ports:
19
+        - containerPort: 80
20
+      - name: sidecar
21
+        image: quay.io/weaveworks/sidecar:master-a000001
22
+        args:
23
+        - -addr=:8080
24
+        ports:
25
+        - containerPort: 8080

Loading…
Cancel
Save