Skip to content

Commit

Permalink
Avoid escaping HTML when decoding JSON in reload test
Browse files Browse the repository at this point in the history
  • Loading branch information
wallyqs committed Apr 15, 2019
1 parent 404f90e commit 35ed435
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
2 changes: 0 additions & 2 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"strings"
"time"

"github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -332,7 +331,6 @@ func (c *Cluster) createPod() (*v1.Pod, error) {

// Create the pod.
pod := kubernetesutil.NewNatsPodSpec(c.cluster.Namespace, name, c.cluster.Name, c.cluster.Spec, c.cluster.AsOwner())
spew.Dump(pod)
pod, err = c.config.KubeCli.Pods(c.cluster.Namespace).Create(pod)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func natsPodReloaderContainer(image, tag, pullPolicy, authFilePath string) v1.Co
if authFilePath != "" {
// The volume is mounted as a subdirectory under the NATS config.
af := filepath.Join(constants.ConfigMapMountPath, authFilePath)
container.Command = append(container.Command, "-authfile", af)
container.Command = append(container.Command, "-c", af)
}
return container
}
Expand Down
51 changes: 39 additions & 12 deletions test/e2e/config_reload_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build e2e

// Copyright 2017 The nats-operator Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,14 +15,14 @@
package e2e

import (
"bytes"
"context"
"encoding/json"
"strings"
"testing"

v1 "k8s.io/api/core/v1"

"github.com/davecgh/go-spew/spew"
"github.com/nats-io/go-nats"
natsv1alpha2 "github.com/nats-io/nats-operator/pkg/apis/nats/v1alpha2"
natsconf "github.com/nats-io/nats-operator/pkg/conf"
Expand Down Expand Up @@ -124,17 +122,23 @@ func TestConfigReloadOnClientAuthFileChange(t *testing.T) {
f.Require(t, framework.ShareProcessNamespace)

ConfigReloadTestHelper(t, func(natsCluster *natsv1alpha2.NatsCluster, cas *v1.Secret) {
natsCluster.Spec.ServerImage = "synadia/nats-server"
natsCluster.Spec.Version = "edge-v2.0.0-RC5"

natsCluster.Spec.Auth = &natsv1alpha2.AuthConfig{
// Use the secret created above for client authentication.
ClientsAuthFile: "/authconfig/auth.json",
ClientsAuthFile: "authconfig/auth.json",
}
natsCluster.Spec.Pod = &natsv1alpha2.PodPolicy{
// Enable configuration reloading.
EnableConfigReload: true,
EnableConfigReload: true,
ReloaderImage: "wallyqs/nats-server-config-reloader",
ReloaderImageTag: "0.4.5-v1alpha2",
ReloaderImagePullPolicy: "Always",
VolumeMounts: []v1.VolumeMount{
v1.VolumeMount{
Name: "authconfig",
MountPath: "/authconfig",
MountPath: "/etc/nats-config/authconfig",
},
},
}
Expand Down Expand Up @@ -211,11 +215,24 @@ func ConfigReloadTestHelper(t *testing.T, customizer NatsClusterCustomizerWSecre
},
},
}
// Serialize the object containing authentication data.
if d, err = json.Marshal(auth); err != nil {
// Serialize the object containing authentication data,
// we are using wildcard so need to unescape the HTML
// which the JSON encoder does by default...
buf := &bytes.Buffer{}
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
err = encoder.Encode(auth)
if err != nil {
t.Fatal(err)
}
buf2 := &bytes.Buffer{}
err = json.Indent(buf2, buf.Bytes(), "", " ")
if err != nil {
t.Fatal(err)
}

// Create a secret containing authentication data.
d = buf2.Bytes()
if cas, err = f.CreateSecret(f.Namespace, "data", d); err != nil {
t.Fatal(err)
}
Expand All @@ -229,7 +246,6 @@ func ConfigReloadTestHelper(t *testing.T, customizer NatsClusterCustomizerWSecre
// Create a NatsCluster resource with a single member, having configuration reloading enabled and using the secret above for client authentication.
natsCluster, err = f.CreateCluster(f.Namespace, "test-nats-", size, version, func(natsCluster *natsv1alpha2.NatsCluster) {
customizer(natsCluster, cas)
t.Log(spew.Sdump(natsCluster))
})

if err != nil {
Expand Down Expand Up @@ -259,12 +275,23 @@ func ConfigReloadTestHelper(t *testing.T, customizer NatsClusterCustomizerWSecre

// Remove "user1" from the list of allowed users.
auth.Users = auth.Users[1:]
// Serialize the object containing authentication data.
if d, err = json.Marshal(auth); err != nil {

// Serialize the object containing authentication data again.
buf = &bytes.Buffer{}
encoder = json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
err = encoder.Encode(auth)
if err != nil {
t.Fatal(err)
}
buf2 = &bytes.Buffer{}
err = json.Indent(buf2, buf.Bytes(), "", " ")
if err != nil {
t.Fatal(err)
}

// Update the client authentication secret with the new contents.
cas.Data["data"] = d
cas.Data["data"] = buf2.Bytes()
if cas, err = f.PatchSecret(cas); err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 35ed435

Please sign in to comment.