Skip to content

Commit

Permalink
feat(e2e): adding generated configmap checks
Browse files Browse the repository at this point in the history
Ref #2320
  • Loading branch information
squakez committed Jan 7, 2022
1 parent 8ebad22 commit fd6e0cc
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
45 changes: 45 additions & 0 deletions e2e/common/cli/dev_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ package common

import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"testing"

. "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"

. "github.com/apache/camel-k/e2e/support"
"github.com/apache/camel-k/e2e/support/util"
Expand Down Expand Up @@ -87,5 +90,47 @@ func TestRunDevMode(t *testing.T) {

Eventually(logScanner.IsFound("Magicstring!"), TestTimeoutMedium).Should(BeTrue())
})

t.Run("Dev mode resource file generated configmap", func(t *testing.T) {
var tmpFile *os.File
var err error
if tmpFile, err = ioutil.TempFile("", "camel-k-"); err != nil {
t.Error(err)
}
assert.Nil(t, tmpFile.Close())
assert.Nil(t, ioutil.WriteFile(tmpFile.Name(), []byte("Hello from test!"), 0o644))

RegisterTestingT(t)
ctx, cancel := context.WithCancel(TestContext)
defer cancel()
piper, pipew := io.Pipe()
defer pipew.Close()
defer piper.Close()

file := util.MakeTempCopy(t, "../config/files/resource-file-location-route.groovy")

kamelRun := KamelWithContext(ctx, "run", "-n", ns, file, "--dev", "--resource", fmt.Sprintf("file:%s@/tmp/file.txt", tmpFile.Name()))
kamelRun.SetOut(pipew)

logScanner := util.NewLogScanner(ctx, piper, `integration "resource-file-location-route" in phase Running`,
"Hello from test!", "Goodbye from test!")

args := os.Args
defer func() { os.Args = args }()
os.Args = []string{"kamel", "run", "-n", ns, file, "--dev", "--resource", fmt.Sprintf("file:%s@/tmp/file.txt", tmpFile.Name())}
go kamelRun.Execute()

Eventually(logScanner.IsFound(`integration "resource-file-location-route" in phase Running`), TestTimeoutMedium).Should(BeTrue())
Eventually(logScanner.IsFound("Hello from test!"), TestTimeoutMedium).Should(BeTrue())
Expect(logScanner.IsFound("Goodbye from test!")()).To(BeFalse())

// cool, now let's change the file to confirm the sync take place
assert.Nil(t, ioutil.WriteFile(tmpFile.Name(), []byte("Goodbye from test!"), 0o644))
Eventually(logScanner.IsFound("Goodbye from test!"), TestTimeoutMedium).Should(BeTrue())

Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
// When the integration is deleted, then, also the autogenerated configmaps must be cleaned
Eventually(AutogeneratedConfigmapsCount(ns), TestTimeoutShort).Should(Equal(0))
})
})
}
23 changes: 23 additions & 0 deletions e2e/common/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ limitations under the License.
package resources

import (
"fmt"
"io/ioutil"
"os"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -242,5 +244,26 @@ func TestRunConfigExamples(t *testing.T) {
Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})

// Resource File: generated configmap must be deleted when Integration is deleted

t.Run("Plain text sync resource file generated configmap", func(t *testing.T) {
var tmpFile *os.File
var err error
if tmpFile, err = ioutil.TempFile("", "camel-k-"); err != nil {
t.Error(err)
}
assert.Nil(t, tmpFile.Close())
assert.Nil(t, ioutil.WriteFile(tmpFile.Name(), []byte("Hello from test!"), 0o644))

Expect(Kamel("run", "-n", ns, "./files/resource-file-location-route.groovy", "--resource", fmt.Sprintf("file:%s@/tmp/file.txt", tmpFile.Name())).Execute()).To(Succeed())
Eventually(IntegrationPodPhase(ns, "resource-file-location-route"), TestTimeoutMedium).Should(Equal(corev1.PodRunning))
Eventually(IntegrationConditionStatus(ns, "resource-file-location-route", v1.IntegrationConditionReady), TestTimeoutMedium).Should(Equal(corev1.ConditionTrue))
Eventually(IntegrationLogs(ns, "resource-file-location-route"), TestTimeoutMedium).Should(ContainSubstring("Hello from test!"))

Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
// When the integration is deleted, then, also the autogenerated configmaps must be cleaned
Eventually(AutogeneratedConfigmapsCount(ns), TestTimeoutShort).Should(Equal(0))
})

})
}
20 changes: 20 additions & 0 deletions e2e/support/test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,26 @@ func Configmap(ns string, name string) func() *corev1.ConfigMap {
}
}

func AutogeneratedConfigmapsCount(ns string) func() int {
return func() int {
lst := corev1.ConfigMapList{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: corev1.SchemeGroupVersion.String(),
},
}
err := TestClient().List(TestContext, &lst,
ctrl.InNamespace(ns),
ctrl.MatchingLabels{
"camel.apache.org/autogenerated": "true",
})
if err != nil {
panic(err)
}
return len(lst.Items)
}
}

func NewPlainTextConfigmap(ns string, name string, data map[string]string) error {
cm := corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/run_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func convertFileToConfigmap(ctx context.Context, c client.Client, resourceSpec v
config.destinationPath = filepath.Dir(config.DestinationPath())
}
genCmName := fmt.Sprintf("cm-%s", hashFrom([]byte(resourceSpec.Content), resourceSpec.RawContent))
cm := kubernetes.NewConfigmap(namespace, genCmName, config.Name(), config.Key(), resourceSpec.Content, resourceSpec.RawContent)
cm := kubernetes.NewConfigmap(namespace, genCmName, filepath.Base(config.Name()), config.Key(), resourceSpec.Content, resourceSpec.RawContent)
err := c.Create(ctx, cm)
if err != nil {
if k8serrors.IsAlreadyExists(err) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/kubernetes/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func NewConfigmap(namespace, cmName, originalFilename string, generatedKey strin
Name: cmName,
Namespace: namespace,
Labels: map[string]string{
"camel.apache.org/original-path": originalFilename,
"camel.apache.org/filename": originalFilename,
},
},
Immutable: &immutable,
Expand Down

0 comments on commit fd6e0cc

Please sign in to comment.