Skip to content

Commit

Permalink
Add test directory
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanBlackburn committed Apr 7, 2024
1 parent 0cd2eba commit 6ec4d8c
Show file tree
Hide file tree
Showing 5 changed files with 468 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# .github/workflows/tests.yml
name: tests

jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: stable
- name: Run Tests
289 changes: 289 additions & 0 deletions tests/end2end_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
package tests

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

"github.com/santiago-labs/telophasecli/lib/awsorgs"
"github.com/santiago-labs/telophasecli/lib/ymlparser"
"github.com/santiago-labs/telophasecli/resource"
"github.com/stretchr/testify/assert"
)

type E2ETestCase struct {
Name string
OrgYaml string
Expected *resource.OrganizationUnit
}

var tests = []E2ETestCase{
{
Name: "Test that we can create OUs",
OrgYaml: `
Organization:
Name: root
OrganizationUnits:
- Name: ProductionTenants
- Name: Development
Accounts:
- AccountName: master
Email: [email protected]
`,
Expected: &resource.OrganizationUnit{
OUName: "root",
ChildOUs: []*resource.OrganizationUnit{
{
OUName: "ProductionTenants",
},
{
OUName: "Development",
},
},
Accounts: []*resource.Account{
{
AccountName: "master",
Email: "[email protected]",
},
},
},
},
{
Name: "Test that we can create nested OUs",
OrgYaml: `
Organization:
Name: root
OrganizationUnits:
- Name: ProductionTenants
OrganizationUnits:
- Name: ProductionEU
- Name: ProductionUS
- Name: Development
OrganizationUnits:
- Name: DevEU
- Name: DevUS
Accounts:
- AccountName: master
Email: [email protected]
`,
Expected: &resource.OrganizationUnit{
OUName: "root",
ChildOUs: []*resource.OrganizationUnit{
{
OUName: "ProductionTenants",
ChildOUs: []*resource.OrganizationUnit{
{
OUName: "ProductionEU",
},
{
OUName: "ProductionUS",
},
},
},
{
OUName: "Development",
ChildOUs: []*resource.OrganizationUnit{
{
OUName: "DevEU",
},
{
OUName: "DevUS",
},
},
},
},
Accounts: []*resource.Account{
{
AccountName: "master",
Email: "[email protected]",
},
},
},
},
{
Name: "Test that we can create accounts",
OrgYaml: `
Organization:
Name: root
Accounts:
- AccountName: master
Email: [email protected]
- AccountName: test1
Email: [email protected]
- AccountName: test2
Email: [email protected]
`,
Expected: &resource.OrganizationUnit{
OUName: "root",
Accounts: []*resource.Account{
{
AccountName: "master",
Email: "[email protected]",
},
{
AccountName: "test1",
Email: "[email protected]",
},
{
AccountName: "test2",
Email: "[email protected]",
},
},
},
},
{
Name: "Test that we can create accounts in OUs",
OrgYaml: `
Organization:
Name: root
OrganizationUnits:
- Name: ProductionTenants
Accounts:
- AccountName: test1
Email: [email protected]
OrganizationUnits:
- Name: ProductionEU
- Name: ProductionUS
Accounts:
- AccountName: test2
Email: [email protected]
- Name: Development
OrganizationUnits:
- Name: DevEU
Accounts:
- AccountName: test3
Email: [email protected]
- AccountName: test4
Email: [email protected]
- AccountName: test5
Email: [email protected]
- Name: DevUS
Accounts:
- AccountName: test6
Email: [email protected]
- AccountName: test7
Email: [email protected]
Accounts:
- AccountName: master
Email: [email protected]
- AccountName: test8
Email: [email protected]
- AccountName: test9
Email: [email protected]
`,
Expected: &resource.OrganizationUnit{
OUName: "root",
ChildOUs: []*resource.OrganizationUnit{
{
OUName: "ProductionTenants",
Accounts: []*resource.Account{
{
AccountName: "test1",
Email: "[email protected]",
},
},
ChildOUs: []*resource.OrganizationUnit{
{
OUName: "ProductionEU",
},
{
OUName: "ProductionUS",
Accounts: []*resource.Account{
{
AccountName: "test2",
Email: "[email protected]",
},
},
},
},
},
{
OUName: "Development",
ChildOUs: []*resource.OrganizationUnit{
{
OUName: "DevEU",
Accounts: []*resource.Account{
{
AccountName: "test3",
Email: "[email protected]",
},
{
AccountName: "test4",
Email: "[email protected]",
},
{
AccountName: "test5",
Email: "[email protected]",
},
},
},
{
OUName: "DevUS",
Accounts: []*resource.Account{
{
AccountName: "test6",
Email: "[email protected]",
},
{
AccountName: "test7",
Email: "[email protected]",
},
},
},
},
},
},
Accounts: []*resource.Account{
{
AccountName: "master",
Email: "[email protected]",
},
{
AccountName: "test8",
Email: "[email protected]",
},
{
AccountName: "test9",
Email: "[email protected]",
},
},
},
},
}

func TestEndToEnd(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("Recovered from panic: %v", r)
}
}()

for _, test := range tests {
setupTest()

fmt.Printf("Running test: %s\n", test.Name)
err := ioutil.WriteFile("organization.yml", []byte(test.OrgYaml), 0644)
assert.NoError(t, err, "Failed to write organization.yml")

parsedOrg, err := ymlparser.ParseOrganizationV2("organization.yml")
assert.NoError(t, err, "Failed to parse organization.yml")

compareOrganizationUnits(t, test.Expected, parsedOrg)

cmd := exec.Command("bash", "-c", "../telophasecli deploy")
_, stderr, err := runCmd(cmd)
assert.NoError(t, err, fmt.Sprintf("Failed to run telophasecli deploy. STDERR \n %s \n", stderr))

ctx := context.Background()
orgClient := awsorgs.New()
rootId, err := orgClient.GetRootId()
assert.NoError(t, err, "Failed to fetch rootId")

fetchedOrg, err := orgClient.FetchOUAndDescendents(ctx, rootId, "000000000000")
assert.NoError(t, err, "Failed to fetch rootOU")

compareOrganizationUnits(t, test.Expected, &fetchedOrg)
}
}
Loading

0 comments on commit 6ec4d8c

Please sign in to comment.