Skip to content

Commit

Permalink
add test condition, rename variables for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismarget-j committed Aug 9, 2023
1 parent 1bccbcc commit 62c1670
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 54 deletions.
47 changes: 22 additions & 25 deletions apstra/two_stage_l3_clos_configlet.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,61 @@ const (
)

type rawTwoStageL3ClosConfigletData struct {
Data rawConfigletData `json:"configlet"`
Condition string `json:"condition"`
Label string `json:"label"`
Condition string `json:"condition"`
Data rawConfigletData `json:"configlet"`
}

type TwoStageL3ClosConfigletData struct {
Data ConfigletData
Condition string
Label string
Condition string
Data *ConfigletData
}

type rawTwoStageL3ClosConfiglet struct {
Data rawConfigletData `json:"configlet"`
Id ObjectId `json:"id"`
Condition string `json:"condition"`
Label string `json:"label"`
Data rawConfigletData `json:"configlet"`
}

type TwoStageL3ClosConfiglet struct {
Data TwoStageL3ClosConfigletData
Id ObjectId
Data *TwoStageL3ClosConfigletData
}

func (o *TwoStageL3ClosConfigletData) raw() *rawTwoStageL3ClosConfigletData {
rawtc := rawTwoStageL3ClosConfigletData{}
rawtc.Data = *o.Data.raw()
rawtc.Condition = o.Condition
rawtc.Label = o.Label
return &rawtc
return &rawTwoStageL3ClosConfigletData{
Label: o.Label,
Condition: o.Condition,
Data: *o.Data.raw(),
}
}

func (o *TwoStageL3ClosConfiglet) raw() *rawTwoStageL3ClosConfiglet {
d := o.Data.raw()
return &rawTwoStageL3ClosConfiglet{
Data: rawConfigletData{
RefArchs: d.Data.RefArchs,
Generators: d.Data.Generators,
DisplayName: d.Data.DisplayName,
},
Id: o.Id,
Condition: d.Condition,
Label: d.Label,
Condition: d.Condition,
Data: d.Data,
}
}

func (o *rawTwoStageL3ClosConfiglet) polish() (*TwoStageL3ClosConfiglet, error) {
c := TwoStageL3ClosConfiglet{}
c.Id = o.Id
d, err := o.Data.polish()
if err != nil {
return nil, err
}
c.Data = TwoStageL3ClosConfigletData{
Data: *d,
Condition: o.Condition,
Label: o.Label,
}
return &c, err

return &TwoStageL3ClosConfiglet{
Id: o.Id,
Data: &TwoStageL3ClosConfigletData{
Data: d,
Condition: o.Condition,
Label: o.Label,
},
}, nil
}

func (o *TwoStageL3ClosClient) getAllConfiglets(ctx context.Context) ([]rawTwoStageL3ClosConfiglet, error) {
Expand Down
67 changes: 38 additions & 29 deletions apstra/two_stage_l3_clos_configlet_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,39 @@ package apstra

import (
"context"
"errors"
"log"
"testing"
)

func TestImportGetUpdateGetDeleteConfiglet(t *testing.T) {
clients, err := getTestClients(context.Background(), t)
ctx := context.Background()
clients, err := getTestClients(ctx, t)
if err != nil {
t.Fatal(err)
}
var cr ConfigletData
var cg []ConfigletGenerator
var refarchs []RefDesign
cg = append(cg, ConfigletGenerator{
ConfigStyle: PlatformOSJunos,
Section: ConfigletSectionSystem,
TemplateText: "interfaces {\n {% if 'leaf1' in hostname %}\n xe-0/0/3 {\n disable;\n }\n {% endif %}\n {% if 'leaf2' in hostname %}\n xe-0/0/2 {\n disable;\n }\n {% endif %}\n}",
})
refarchs = append(refarchs, RefDesignTwoStageL3Clos)
cr = ConfigletData{

configletData := ConfigletData{
DisplayName: "TestImportConfiglet",
RefArchs: refarchs,
Generators: cg,
RefArchs: []RefDesign{RefDesignTwoStageL3Clos},
Generators: []ConfigletGenerator{{
ConfigStyle: PlatformOSJunos,
Section: ConfigletSectionSystem,
TemplateText: "interfaces {\n {% if 'leaf1' in hostname %}\n xe-0/0/3 {\n disable;\n }\n {% endif %}\n {% if 'leaf2' in hostname %}\n xe-0/0/2 {\n disable;\n }\n {% endif %}\n}",
}},
}
ctx := context.TODO()

for clientName, client := range clients {
// Create Configlet
CatConfId, err := client.client.CreateConfiglet(ctx, &cr)
catalogConfigletId, err := client.client.CreateConfiglet(ctx, &configletData)
if err != nil {
t.Fatal(err)
}
defer func() {
client.client.DeleteConfiglet(ctx, CatConfId)
err := client.client.DeleteConfiglet(ctx, catalogConfigletId)
if err != nil {
t.Fatal(err)
}
}()

bpClient, bpDel := testBlueprintA(ctx, t, client.client)
Expand All @@ -47,36 +48,44 @@ func TestImportGetUpdateGetDeleteConfiglet(t *testing.T) {
}
}()

log.Printf("testing ImportConfigletById() against %s %s (%s)", client.clientType, clientName,
client.client.ApiVersion())
icfg_id, err := bpClient.ImportConfigletById(ctx, CatConfId, "role in [\"spine\", \"leaf\"]", "")
log.Printf("%s", icfg_id)
log.Printf("testing ImportConfigletById() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion())
bpConfigletId, err := bpClient.ImportConfigletById(ctx, catalogConfigletId, `role in ["spine", "leaf"]`, "")
log.Printf("%s", bpConfigletId)
if err != nil {
t.Fatal(err)
}

log.Printf("testing GetConfiglet() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion())
icfg, err := bpClient.GetConfiglet(ctx, icfg_id)
log.Println(icfg)
_, err = bpClient.GetConfiglet(ctx, bpConfigletId)
if err != nil {
t.Fatal(err)
}

log.Printf("testing DeleteConfiglet() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion())
err = bpClient.DeleteConfiglet(ctx, icfg_id)
err = bpClient.DeleteConfiglet(ctx, bpConfigletId)
if err != nil {
t.Fatal(err)
}

cr.DisplayName = "ImportDirect"
_, err = bpClient.GetConfiglet(ctx, bpConfigletId)
if err == nil {
t.Fatal("fetch configlet after delete should have produced an error")
} else {
var ace ApstraClientErr
if !errors.As(err, &ace) || ace.Type() != ErrNotfound {
t.Fatal("fetch configlet after delete should have produced ErrNotFound")
}
}

configletData.DisplayName = "ImportDirect"
log.Printf("testing ImportConfiglet() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion())
c := TwoStageL3ClosConfigletData{
Data: cr,
Data: &configletData,
Condition: "role in [\"spine\", \"leaf\"]",
Label: "",
}
icfg_id, err = bpClient.CreateConfiglet(ctx, &c)
log.Printf("%s", icfg_id)
bpConfigletId, err = bpClient.CreateConfiglet(ctx, &c)
log.Printf("%s", bpConfigletId)
if err != nil {
t.Fatal(err)
}
Expand All @@ -96,7 +105,7 @@ func TestImportGetUpdateGetDeleteConfiglet(t *testing.T) {
t.Fatal(err)
}
log.Printf("testing GetConfiglet() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion())
icfg2, err := bpClient.GetConfiglet(ctx, icfg_id)
icfg2, err := bpClient.GetConfiglet(ctx, bpConfigletId)
log.Println(icfg2)
if err != nil {
t.Fatal(err)
Expand All @@ -108,7 +117,7 @@ func TestImportGetUpdateGetDeleteConfiglet(t *testing.T) {
t.Fatal("Condition Change Failed")
}
log.Printf("testing DeleteConfiglet() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion())
err = bpClient.DeleteConfiglet(ctx, icfg_id)
err = bpClient.DeleteConfiglet(ctx, bpConfigletId)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 62c1670

Please sign in to comment.