Skip to content

Commit

Permalink
Fix agentctl tests (#1713)
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrej-fabry committed Aug 27, 2020
1 parent 8d5e501 commit 6cd31bd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 58 deletions.
5 changes: 3 additions & 2 deletions cmd/agentctl/commands/dump.go
Expand Up @@ -191,10 +191,11 @@ func printDumpTable(out io.Writer, dump []api.KVWithMetadata) {
"Model", "Origin", "Value", "Metadata", "Key",
})
table.SetAutoMergeCells(true)
table.SetAutoWrapText(false)
table.SetRowLine(true)

for _, d := range dump {
val := proto.MarshalTextString(d.Value)
val := yamlTmpl(d.Value)
var meta string
if d.Metadata != nil {
meta = yamlTmpl(d.Metadata)
Expand All @@ -211,7 +212,7 @@ func printDumpTable(out io.Writer, dump []api.KVWithMetadata) {
name = d.Key
}
}
val = fmt.Sprintf("[%s]\n%s", proto.MessageName(d.Value), val)
val = fmt.Sprintf("# %s\n%s", proto.MessageName(d.Value), val)
var row []string
row = []string{
model,
Expand Down
95 changes: 39 additions & 56 deletions tests/e2e/100_agentctl_test.go
Expand Up @@ -68,7 +68,7 @@ func TestAgentCtlCommands(t *testing.T) {
name: "Test `dump` action with bad model",
cmd: "dump NoSuchModel",
expectErr: true,
expectInStderr: "no models found for [\"NoSuchModel\"]",
expectInStderr: "no matching models found for [\"NoSuchModel\"]",
},
{
name: "Test `dump` action with one bad model",
Expand All @@ -83,7 +83,7 @@ func TestAgentCtlCommands(t *testing.T) {
{
name: "Test `dump --view=NB` action",
cmd: "dump vpp.interfaces --view=NB",
expectReStdout: `KEY\s+VALUE\s+ORIGIN\s+METADATA`,
expectReStdout: `MODEL[\s\|]+ORIGIN[\s\|]+VALUE[\s\|]+METADATA`,
},
{
name: "Test `dump --view=cached` action",
Expand All @@ -100,7 +100,6 @@ func TestAgentCtlCommands(t *testing.T) {
cmd: "dump vpp.interfaces -f=yaml",
expectReStdout: `Value:\s+name: UNTAGGED-local0`,
},

{
name: "Test `dump` with custom format",
cmd: `dump vpp.interfaces -f "{{range.}}Name:{{.Value.Name}}{{end}}"`,
Expand Down Expand Up @@ -241,37 +240,26 @@ func TestAgentCtlCommands(t *testing.T) {
test.cmd, err, stderr,
)
}

// Check STDOUT:
if test.expectNotEmptyStdout {
Expect(len(stdout)).To(Not(BeZero()),
"Stdout should not be empty\n",
)
}

if test.expectStdout != "" {
Expect(stdout).To(Equal(test.expectStdout),
"Want stdout: \n%s\nGot stdout: \n%s\n",
test.expectStdout, stdout,
"Expected output not equal stdout",
)
}

if test.expectInStdout != "" {
Expect(strings.Contains(stdout, test.expectInStdout)).To(BeTrue(),
"Want in stdout: \n%s\nGot stdout: \n%s\n",
test.expectInStdout, stdout,
)
Expect(stdout).To(ContainSubstring(test.expectInStdout),
"Expected string not found in stdout")
}

if test.expectReStdout != "" {
matched, err = regexp.MatchString(test.expectReStdout, stdout)
Expect(err).To(BeNil())
Expect(matched).To(BeTrue(),
"Want stdout to contain any match of the regular expression: \n`%s`\nGot stdout: \n%s\n",
test.expectReStdout, stdout,
)
Expect(matched).To(BeTrue(), "Expect regexp to match for stdout")
}

// Check STDERR:
if test.expectInStderr != "" {
Expect(strings.Contains(stderr, test.expectInStderr)).To(BeTrue(),
Expand All @@ -283,21 +271,6 @@ func TestAgentCtlCommands(t *testing.T) {
}
}

func createFileWithContent(path, content string) error {
f, err := os.Create(path)
if err != nil {
return err
}
w := bufio.NewWriter(f)
_, err = w.WriteString(content)
if err != nil {
return err
}
w.Flush()

return nil
}

func TestAgentCtlSecureGrpcWithClientCertRequired(t *testing.T) {
// WARNING: Do not use grpc connection created in `setupE2E` in
// this test (though I don't know why you would but anyway).
Expand Down Expand Up @@ -326,7 +299,6 @@ func TestAgentCtlSecureGrpcWithClientCertRequired(t *testing.T) {
Expect(strings.Contains(stderr, "rpc error")).To(BeTrue(),
"Want in stderr: \n\"rpc error\"\nGot stderr: \n%s\n", stderr,
)
t.Log("PASSED")

t.Log("Try with TLS enabled via flag --insecure-tls, but without cert and key (note: server configured to check those files)")
_, stderr, err = ctx.execCmd(
Expand All @@ -336,7 +308,6 @@ func TestAgentCtlSecureGrpcWithClientCertRequired(t *testing.T) {
Expect(strings.Contains(stderr, "rpc error")).To(BeTrue(),
"Want in stderr: \n\"rpc error\"\nGot stderr: \n%s\n", stderr,
)
t.Log("PASSED")

t.Log("Try with fully configured TLS via config file")
stdout, stderr, err := ctx.execCmd(
Expand All @@ -346,7 +317,6 @@ func TestAgentCtlSecureGrpcWithClientCertRequired(t *testing.T) {
"Should not fail. Got err: %v\nStderr:\n%s\n", err, stderr,
)
Expect(len(stdout)).To(Not(BeZero()))
t.Log("PASSED")
}

func TestAgentCtlSecureGrpc(t *testing.T) {
Expand Down Expand Up @@ -374,10 +344,8 @@ func TestAgentCtlSecureGrpc(t *testing.T) {
"/agentctl", "--debug", "dump", "vpp.interfaces",
)
Expect(err).To(Not(BeNil()))
Expect(strings.Contains(stderr, "rpc error")).To(BeTrue(),
"Want in stderr: \n\"rpc error\"\nGot stderr: \n%s\n", stderr,
)
t.Log("PASSED")
Expect(stderr).To(ContainSubstring("rpc error"),
"Expected string not found in stderr")

t.Log("Try with TLS enabled via flag --insecure-tls. Should work because server is not configured to check client certs.")
stdout, stderr, err := ctx.execCmd(
Expand All @@ -387,7 +355,6 @@ func TestAgentCtlSecureGrpc(t *testing.T) {
"Should not fail. Got err: %v\nStderr:\n%s\n", err, stderr,
)
Expect(len(stdout)).To(Not(BeZero()))
t.Log("PASSED")

t.Log("Try with fully configured TLS via config file")
stdout, stderr, err = ctx.execCmd(
Expand All @@ -396,8 +363,7 @@ func TestAgentCtlSecureGrpc(t *testing.T) {
Expect(err).To(BeNil(),
"Should not fail. Got err: %v\nStderr:\n%s\n", err, stderr,
)
Expect(len(stdout)).To(Not(BeZero()))
t.Log("PASSED")
Expect(stdout).ToNot(BeEmpty())
}

func TestAgentCtlSecureETCD(t *testing.T) {
Expand All @@ -406,18 +372,35 @@ func TestAgentCtlSecureETCD(t *testing.T) {
etcdID := ctx.setupETCD()
defer ctx.teardownETCD(etcdID)

t.Log("Try without any TLS")
_, _, err := ctx.execCmd("/agentctl", "--debug", "kvdb", "list")
Expect(err).To(Not(BeNil()))
t.Log("PASSED")

t.Log("Try with TLS enabled via flag --insecure-tls, but without cert and key (note: server configured to check those files)")
_, _, err = ctx.execCmd("/agentctl", "--debug", "--insecure-tls", "kvdb", "list")
Expect(err).To(Not(BeNil()))
t.Log("PASSED")
// test without any TLS
t.Run("no TLS", func(t *testing.T) {
_, _, err := ctx.execCmd("/agentctl", "--debug", "kvdb", "list")
Expect(err).To(Not(BeNil()))
})

// test with TLS enabled via flag --insecure-tls, but without cert and key (note: server configured to check those files)
t.Run("insecure TLS", func(t *testing.T) {
_, _, err := ctx.execCmd("/agentctl", "--debug", "--insecure-tls", "kvdb", "list")
Expect(err).To(Not(BeNil()))
})

// test with fully configured TLS via config file
t.Run("fully cofigured TLS", func(t *testing.T) {
_, stderr, err := ctx.execCmd("/agentctl", "--debug", "--config-dir=/etc/.agentctl", "kvdb", "list")
Expect(err).To(BeNil(), "Should not fail. Got err: %v\nStderr:\n%s\n", err, stderr)
})
}

t.Log("Try with fully configured TLS via config file")
_, stderr, err := ctx.execCmd("/agentctl", "--debug", "--config-dir=/etc/.agentctl", "kvdb", "list")
Expect(err).To(BeNil(), "Should not fail. Got err: %v\nStderr:\n%s\n", err, stderr)
t.Log("PASSED")
func createFileWithContent(path, content string) error {
f, err := os.Create(path)
if err != nil {
return err
}
w := bufio.NewWriter(f)
_, err = w.WriteString(content)
if err != nil {
return err
}
w.Flush()
return nil
}

0 comments on commit 6cd31bd

Please sign in to comment.