-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
113 lines (87 loc) · 2.83 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"testing"
)
var expectedCommands = []string{"kubectl get ns", "echo \"Hello World!\"\nkubectl get pods", "kubectl get ns"}
var mdFile = []string{
"# Hello World",
"<!-- command -->",
"```kubectl get ns```",
"```kubectl get pods```",
"<!-- var DT_TENANT -->",
"<!-- TODO: Add support for code blocks with language -->",
"<!-- bash\necho \"Hello World!\"\nkubectl get pods\n-->",
"<!-- bash\nkubectl get ns\n-->",
}
var bashOutputFile = []string{
"#!/bin/bash",
"set -e",
`if [ -z "$DT_TENANT" ]; then`,
`echo "Please supply a value for the environment variable DT_TENANT"`,
`exit 1`,
`fi`,
"kubectl get ns",
`echo "Hello World!"`,
"kubectl get pods",
"kubectl get ns",
}
func TestGetCommandsAndActionsWithBashInput(t *testing.T) {
markdownFile := fmt.Sprintln(mdFile)
htmlFile, err := convertMarkdownToHTML([]byte(markdownFile))
require.NoError(t, err, "Expected no error")
commands, err := getCommandsAndActions(htmlFile)
require.NoError(t, err, "Expected no error")
require.Equal(t, expectedCommands, commands)
}
func TestProcessComment(t *testing.T) {
require := require.New(t)
for _, tt := range []struct {
Input string
Result string
}{
{Input: "<!-- bash echo test -->", Result: "echo test"},
{Input: "<!-- bash kubectl get pods -->", Result: "kubectl get pods"},
{Input: "<!-- bash wait_for_deployment_with_image_in_namespace -->", Result: "wait_for_deployment_with_image_in_namespace"},
{Input: "<!-- var DT_TENANT -->", Result: ""},
{Input: "<!-- var DT_API_TOKEN -->", Result: ""},
} {
param := processComment(tt.Input)
require.Equal(tt.Result, param)
}
}
func TestConvertTutorialIntoBashFile(t *testing.T) {
require := require.New(t)
// Creating markdown file for test
markdownFile := fmt.Sprintln(mdFile)
filepath := "testfile.md"
outputFile := "testoutput.sh"
deleteFile, err := createAndDeleteMarkdownFile(filepath, markdownFile)
if deleteFile != nil {
defer deleteFile()
}
require.NoError(err, fmt.Sprintf("Expected no error but got %s", err))
// Converting markdown file into bash script
err = convertTutorialIntoBashScript(filepath, outputFile)
require.NoError(err, fmt.Sprintf("Expected no error but got %s", err))
// Reading the output of the bash file
bashFile, err := ioutil.ReadFile(outputFile)
require.NoError(err, fmt.Sprintf("Expected no error but got %s", err))
for _, item := range bashOutputFile {
require.Contains(string(bashFile), item, fmt.Sprintf("Bash file: %s doesn't contain output item %s", string(bashFile), item))
}
// Remove output file
os.Remove(outputFile)
}
func createAndDeleteMarkdownFile(path, content string) (func(), error) {
err := ioutil.WriteFile(path, []byte(content), 0644)
if err != nil {
return nil, err
}
return func() {
os.Remove(path)
}, nil
}