diff --git a/loadbalance_content_rewriting_test.go b/loadbalance_content_rewriting_test.go index 117822a..5fbfac7 100644 --- a/loadbalance_content_rewriting_test.go +++ b/loadbalance_content_rewriting_test.go @@ -6,6 +6,10 @@ import ( ) func TestClient_LoadbalanceGetContentRewritings(t *testing.T) { + if os.Getenv("TEST_LENS") != "true" { + t.Skip() + } + client, err := NewClientHelper() if err != nil { t.Fatal(err) diff --git a/loadbalance_content_routing_test.go b/loadbalance_content_routing_test.go index bd92bcf..1f77a80 100644 --- a/loadbalance_content_routing_test.go +++ b/loadbalance_content_routing_test.go @@ -6,6 +6,10 @@ import ( ) func TestClient_LoadbalanceGetContentRoutings(t *testing.T) { + if os.Getenv("TEST_LENS") != "true" { + t.Skip() + } + client, err := NewClientHelper() if err != nil { t.Fatal(err) diff --git a/loadbalance_pool_test.go b/loadbalance_pool_test.go index b14e385..ddab048 100644 --- a/loadbalance_pool_test.go +++ b/loadbalance_pool_test.go @@ -6,6 +6,10 @@ import ( ) func TestClient_LoadbalanceGetPools(t *testing.T) { + if os.Getenv("TEST_LENS") != "true" { + t.Skip() + } + client, err := NewClientHelper() if err != nil { t.Fatal(err) diff --git a/loadbalance_real_server_test.go b/loadbalance_real_server_test.go index 4a96144..6167653 100644 --- a/loadbalance_real_server_test.go +++ b/loadbalance_real_server_test.go @@ -6,6 +6,9 @@ import ( ) func TestClient_LoadbalanceGetRealServers(t *testing.T) { + if os.Getenv("TEST_LENS") != "true" { + t.Skip() + } client, err := NewClientHelper() if err != nil { diff --git a/loadbalance_test.go b/loadbalance_test.go index 03826e5..7eb2831 100644 --- a/loadbalance_test.go +++ b/loadbalance_test.go @@ -2,13 +2,13 @@ package gofortiadc import ( "testing" + + "github.com/stretchr/testify/require" ) func TestLoadbalance(t *testing.T) { client, err := NewClientHelper() - if err != nil { - t.Fatal(err) - } + require.NoError(t, err, "NewClientHelper") // Create real server reqCreateRealServer := LoadbalanceRealServer{ @@ -17,12 +17,15 @@ func TestLoadbalance(t *testing.T) { Address6: "::", Mkey: "gofortirs01", } - t.Logf("reqCreateRealServer: %+v", reqCreateRealServer) err = client.LoadbalanceCreateRealServer(reqCreateRealServer) - if err != nil { - t.Fatalf("LoadbalanceCreateRealServer failed with error: %s", err) - } + require.NoError(t, err, "LoadbalanceCreateRealServer") + + defer func() { + // Delete real server + err = client.LoadbalanceDeleteRealServer("gofortirs01") + require.NoError(t, err, "LoadbalanceDeleteRealServer") + }() // Update real server reqUpdateRealServer := LoadbalanceRealServer{ @@ -31,12 +34,9 @@ func TestLoadbalance(t *testing.T) { Address6: "::", Mkey: "gofortirs01", } - t.Logf("reqUpdateRealServer: %+v", reqUpdateRealServer) err = client.LoadbalanceUpdateRealServer(reqUpdateRealServer) - if err != nil { - t.Fatalf("LoadbalanceUpdateRealServer failed with error: %s", err) - } + require.NoError(t, err, "LoadbalanceUpdateRealServer") // Create real server pool reqCreateRealServerPool := LoadbalancePool{ @@ -47,12 +47,15 @@ func TestLoadbalance(t *testing.T) { HealthCheckList: "LB_HLTHCK_HTTP LB_HLTHCK_HTTPS", RsProfile: "NONE", } - t.Logf("reqCreateRealServerPool: %+v", reqCreateRealServerPool) err = client.LoadbalanceCreatePool(reqCreateRealServerPool) - if err != nil { - t.Fatalf("LoadbalanceCreatePool failed with error: %s", err) - } + require.NoError(t, err, "LoadbalanceCreatePool") + + defer func() { + // Delete real server pool + err = client.LoadbalanceDeletePool("GOFORTI_POOL") + require.NoError(t, err, "LoadbalanceDeletePool") + }() // Update real server pool reqUpdateRealServerPool := LoadbalancePool{ @@ -63,12 +66,9 @@ func TestLoadbalance(t *testing.T) { HealthCheckList: "LB_HLTHCK_HTTP", RsProfile: "NONE", } - t.Logf("reqUpdateRealServerPool: %+v", reqUpdateRealServerPool) err = client.LoadbalanceUpdatePool(reqUpdateRealServerPool) - if err != nil { - t.Fatalf("LoadbalanceUpdatePool failed with error: %s", err) - } + require.NoError(t, err, "LoadbalanceUpdatePool") // Create real server pool member reqCreateRealServerPoolMember := LoadbalancePoolMember{ @@ -95,12 +95,15 @@ func TestLoadbalance(t *testing.T) { Warmup: "0", Warmrate: "100", } - t.Logf("reqCreateRealServerPoolMember: %+v", reqCreateRealServerPoolMember) err = client.LoadbalanceCreatePoolMember("GOFORTI_POOL", reqCreateRealServerPoolMember) - if err != nil { - t.Fatalf("LoadbalanceCreatePoolMember failed with error: %s", err) - } + require.NoError(t, err, "LoadbalanceCreatePoolMember") + + defer func() { + // Delete real server pool member + err = client.LoadbalanceDeletePoolMember("GOFORTI_POOL", "1") + require.NoError(t, err, "LoadbalanceDeletePoolMember") + }() // Update real server pool member reqUpdateRealServerPoolMember := LoadbalancePoolMember{ @@ -128,91 +131,269 @@ func TestLoadbalance(t *testing.T) { Warmup: "0", Warmrate: "100", } - t.Logf("reqUpdateRealServerPoolMember: %+v", reqUpdateRealServerPoolMember) err = client.LoadbalanceUpdatePoolMember("GOFORTI_POOL", "1", reqUpdateRealServerPoolMember) - if err != nil { - t.Fatalf("LoadbalanceUpdatePoolMember failed with error: %s", err) - } + require.NoError(t, err, "LoadbalanceUpdatePoolMember") // Create virtual server reqCreateVirtualServer := LoadbalanceVirtualServer{ - Status: "enable", - Type: "l4-load-balance", AddrType: "ipv4", Address: "128.1.201.35", - PacketFwdMethod: "NAT", - Port: "80", + Alone: "enable", ConnectionLimit: "10000", - ContentRouting: "disable", - Warmup: "0", - Warmrate: "10", ConnectionRateLimit: "0", - TrafficLog: "enable", - Alone: "enable", - Mkey: "GOFORTI-VS", + ContentRewriting: "disable", + ContentRouting: "disable", + HTTP2HTTPS: "disable", Interface: "port1", - Profile: "LB_PROF_TCP", Method: "LB_METHOD_ROUND_ROBIN", + Mkey: "GOFORTI-VS", + PacketFwdMethod: "NAT", Pool: "GOFORTI_POOL", - HTTP2HTTPS: "enable", + Port: "80", + Profile: "LB_PROF_TCP", + Status: "enable", + TrafficLog: "enable", + Type: "l4-load-balance", + Warmrate: "10", + Warmup: "0", } - t.Logf("reqCreateVirtualServer: %+v", reqCreateVirtualServer) err = client.LoadbalanceCreateVirtualServer(reqCreateVirtualServer) - if err != nil { - t.Fatalf("LoadbalanceCreateVirtualServer failed with error: %s", err) - } + require.NoError(t, err, "LoadbalanceCreateVirtualServer") + + defer func() { + // Delete virtual server + err = client.LoadbalanceDeleteVirtualServer("GOFORTI-VS") + require.NoError(t, err, "LoadbalanceDeleteVirtualServer") + }() // Update virtual server reqUpdateVirtualServer := LoadbalanceVirtualServer{ - Status: "enable", - Type: "l4-load-balance", AddrType: "ipv4", Address: "128.1.201.35", - PacketFwdMethod: "NAT", - Port: "80", + Alone: "enable", ConnectionLimit: "10000", - ContentRouting: "disable", - Warmup: "0", - Warmrate: "10", ConnectionRateLimit: "0", - TrafficLog: "enable", - Alone: "enable", - Mkey: "GOFORTI-VS", + ContentRewriting: "disable", + ContentRouting: "disable", Interface: "port1", - Profile: "LB_PROF_TCP", Method: "LB_METHOD_FASTEST_RESPONSE", + Mkey: "GOFORTI-VS", + PacketFwdMethod: "NAT", Pool: "GOFORTI_POOL", + Port: "80", + Profile: "LB_PROF_TCP", + Status: "enable", + TrafficLog: "enable", + Type: "l4-load-balance", + Warmrate: "10", + Warmup: "0", } - t.Logf("reqUpdateVirtualServer: %+v", reqUpdateVirtualServer) err = client.LoadbalanceUpdateVirtualServer(reqUpdateVirtualServer) - if err != nil { - t.Fatalf("LoadbalanceUpdateVirtualServer failed with error: %s", err) + require.NoError(t, err, "LoadbalanceUpdateVirtualServer") + + // List content rewritings + _, err = client.LoadbalanceGetContentRewritings() + require.NoError(t, err, "LoadbalanceGetContentRewritings") + + // Create content rewriting + reqCW := LoadbalanceContentRewriting{ + Mkey: "gofortirw01", + ActionType: "request", + URLStatus: "enable", + URLContent: "/url", + RefererStatus: "enable", + RefererContent: "http://", + Redirect: "redirect", + Location: "http://", + HeaderName: "header-name", + Comments: "", + Action: "rewrite_http_header", + HostStatus: "enable", + HostContent: "host", } - // Delete virtual server - err = client.LoadbalanceDeleteVirtualServer("GOFORTI-VS") - if err != nil { - t.Fatalf("LoadbalanceDeleteVirtualServer failed with error: %s", err) + err = client.LoadbalanceCreateContentRewriting(reqCW) + require.NoError(t, err, "LoadbalanceCreateContentRewriting") + + defer func() { + // Delete content rewriting + err = client.LoadbalanceDeleteContentRewriting("gofortirw01") + require.NoError(t, err, "LoadbalanceDeleteContentRewriting") + }() + + // Get content rewriting + _, err = client.LoadbalanceGetContentRewriting("gofortirw01") + require.NoError(t, err, "LoadbalanceGetContentRewriting") + + // Update content rewriting + reqCW = LoadbalanceContentRewriting{ + Mkey: "gofortirw01", + ActionType: "request", + URLStatus: "disable", + URLContent: "/url", + RefererStatus: "enable", + RefererContent: "http://foo.bar", + Redirect: "redirect", + Location: "http://", + HeaderName: "header-name", + Comments: "", + Action: "rewrite_http_header", + HostStatus: "disable", + HostContent: "host", } - // Delete real server pool member - err = client.LoadbalanceDeletePoolMember("GOFORTI_POOL", "1") - if err != nil { - t.Fatalf("LoadbalanceDeletePoolMember failed with error: %s", err) + err = client.LoadbalanceUpdateContentRewriting(reqCW) + require.NoError(t, err, "LoadbalanceUpdateContentRewriting") + + // Get content rewriting conditions + _, err = client.LoadbalanceGetContentRewritingConditions("gofortirw01") + require.NoError(t, err, "LoadbalanceGetContentRewritingConditions") + + // Create content rewriting condition + condReqCW := LoadbalanceContentRewritingCondition{ + Mkey: "", + Content: "match", + Ignorecase: "enable", + Object: "http-host-header", + Reverse: "disable", + Type: "string", } - // Delete real server pool - err = client.LoadbalanceDeletePool("GOFORTI_POOL") - if err != nil { - t.Fatalf("LoadbalanceDeletePool failed with error: %s", err) + err = client.LoadbalanceCreateContentRewritingCondition("gofortirw01", condReqCW) + require.NoError(t, err, "LoadbalanceCreateContentRewritingCondition") + + defer func() { + err = client.LoadbalanceDeleteContentRewritingCondition("gofortirw01", "1") + require.NoError(t, err, "LoadbalanceDeleteContentRewritingCondition") + }() + + // Get content rewriting condition + _, err = client.LoadbalanceGetContentRewritingCondition("gofortirw01", "1") + require.NoError(t, err, "LoadbalanceGetContentRewritingCondition") + + // Get content rewriting condition ID + condReqCW = LoadbalanceContentRewritingCondition{ + Mkey: "", + Content: "match", + Ignorecase: "enable", + Object: "http-host-header", + Reverse: "disable", + Type: "string", + } + + id, err := client.LoadbalanceGetContentRewritingConditionID("gofortirw01", condReqCW) + require.NoError(t, err, "LoadbalanceGetContentRewritingConditionID") + require.Equal(t, "1", id, "LoadbalanceGetContentRewritingConditionID") + + // Update content rewriting condition + condReqCW = LoadbalanceContentRewritingCondition{ + Mkey: "1", + Content: "127.0.0.1", + Ignorecase: "disable", + Object: "ip-source-address", + Reverse: "disable", + Type: "string", } - // Delete real server - err = client.LoadbalanceDeleteRealServer("gofortirs01") - if err != nil { - t.Fatalf("LoadbalanceDeleteRealServer failed with error: %s", err) + err = client.LoadbalanceUpdateContentRewritingCondition("gofortirw01", condReqCW) + + // Get content routings + _, err = client.LoadbalanceGetContentRoutings() + require.NoError(t, err, "LoadbalanceGetContentRoutings") + + // Create content routing + reqCR := LoadbalanceContentRouting{ + Mkey: "goforticr01", + Type: "l7-content-routing", + PacketFwdMethod: "inherit", + SourcePoolList: "", + Persistence: "", + PersistenceInherit: "enable", + Method: "", + MethodInherit: "enable", + ConnectionPool: "", + ConnectionPoolInherit: "enable", + Pool: "GOFORTI_POOL", + IP: "0.0.0.0/0", + IP6: "::/0", + Comments: "", + ScheduleList: "disable", + SchedulePoolList: "", + } + + err = client.LoadbalanceCreateContentRouting(reqCR) + require.NoError(t, err, "LoadbalanceCreateContentRouting") + + defer func() { + // Delete content routing + err = client.LoadbalanceDeleteContentRouting("goforticr01") + require.NoError(t, err, "LoadbalanceDeleteContentRouting") + }() + + // Get content routing + _, err = client.LoadbalanceGetContentRouting("goforticr01") + require.NoError(t, err, "LoadbalanceGetContentRouting") + + // Update content routing + reqCR = LoadbalanceContentRouting{ + Mkey: "goforticr01", + Type: "l7-content-routing", + PacketFwdMethod: "inherit", + SourcePoolList: "", + Persistence: "", + PersistenceInherit: "enable", + Method: "LB_METHOD_LEAST_CONNECTION", + MethodInherit: "disable", + ConnectionPool: "", + ConnectionPoolInherit: "enable", + Pool: "GOFORTI_POOL", + IP: "0.0.0.0/0", + IP6: "::/0", + Comments: "", + ScheduleList: "disable", + SchedulePoolList: "", } + + err = client.LoadbalanceUpdateContentRouting(reqCR) + require.NoError(t, err, "LoadbalanceUpdateContentRouting") + + // Get content routing conditions + _, err = client.LoadbalanceGetContentRoutingConditions("goforticr01") + require.NoError(t, err, "LoadbalanceGetContentRoutingConditions") + + // Create content routing condition + condReqCR := LoadbalanceContentRoutingCondition{ + Mkey: "", + Object: "http-host-header", + Type: "string", + Content: "gofortiadc.fakedomain.local", + Reverse: "disable", + } + + err = client.LoadbalanceCreateContentRoutingCondition("goforticr01", condReqCR) + require.NoError(t, err, "LoadbalanceCreateContentRoutingCondition") + + defer func() { + // Delete content routing condition + err = client.LoadbalanceDeleteContentRoutingCondition("goforticr01", "1") + require.NoError(t, err, "LoadbalanceDeleteContentRoutingCondition") + }() + + // Get content routing condition + _, err = client.LoadbalanceGetContentRoutingCondition("goforticr01", "1") + require.NoError(t, err, "LoadbalanceGetContentRoutingCondition") + + // Update content routing condition + condReqCR = LoadbalanceContentRoutingCondition{ + Mkey: "1", + Object: "http-request-url", + Type: "string", + Content: "/goforti.html", + Reverse: "disable", + } + + err = client.LoadbalanceUpdateContentRoutingCondition("goforticr01", condReqCR) } diff --git a/loadbalance_virtual_server_test.go b/loadbalance_virtual_server_test.go index de50a42..bfad23f 100644 --- a/loadbalance_virtual_server_test.go +++ b/loadbalance_virtual_server_test.go @@ -6,6 +6,10 @@ import ( ) func TestClient_LoadbalanceGetVirtualServers(t *testing.T) { + if os.Getenv("TEST_LENS") != "true" { + t.Skip() + } + client, err := NewClientHelper() if err != nil { t.Fatal(err) diff --git a/system_certificate_local_group_member_test.go b/system_certificate_local_group_member_test.go index 6dee152..71eef35 100644 --- a/system_certificate_local_group_member_test.go +++ b/system_certificate_local_group_member_test.go @@ -6,6 +6,9 @@ import ( ) func TestClient_SystemGetLocalCertificateGroupMembers(t *testing.T) { + if os.Getenv("TEST_LENS") != "true" { + t.Skip() + } client, err := NewClientHelper() if err != nil { diff --git a/system_certificate_local_group_test.go b/system_certificate_local_group_test.go index 0cb57d0..fbf9bf1 100644 --- a/system_certificate_local_group_test.go +++ b/system_certificate_local_group_test.go @@ -6,6 +6,9 @@ import ( ) func TestClient_SystemGetLocalCertificateGroups(t *testing.T) { + if os.Getenv("TEST_LENS") != "true" { + t.Skip() + } client, err := NewClientHelper() if err != nil { diff --git a/system_certificate_local_test.go b/system_certificate_local_test.go index de13b4d..917171b 100644 --- a/system_certificate_local_test.go +++ b/system_certificate_local_test.go @@ -13,6 +13,9 @@ import ( ) func TestClient_SystemGetLocalCertificates(t *testing.T) { + if os.Getenv("TEST_LENS") != "true" { + t.Skip() + } client, err := NewClientHelper() if err != nil { diff --git a/system_certificate_test.go b/system_certificate_test.go index 31d922f..fcda636 100644 --- a/system_certificate_test.go +++ b/system_certificate_test.go @@ -2,24 +2,31 @@ package gofortiadc import ( "testing" + + "github.com/stretchr/testify/require" ) func TestSystemCertficate(t *testing.T) { client, err := NewClientHelper() - if err != nil { - t.Fatal(err) - } + require.NoError(t, err, "NewClientHelper") // Create local certificate cert, key, err := generateCertificate() - if err != nil { - t.Fatal(err) - } + require.NoError(t, err, "generateCertificate") err = client.SystemCreateLocalCertificate("gofortiadc", "", cert, key) - if err != nil { - t.Fatalf("SystemCreateLocalCertificate failed with error: %s", err) - } + require.NoError(t, err, "SystemCreateLocalCertificate") + + defer func() { + // Delete local certificate + err = client.SystemDeleteLocalCertificate("gofortiadc") + require.NoError(t, err, "SystemDeleteLocalCertificate") + }() + + // Get local certificate + certRes, err := client.SystemGetLocalCertificate("gofortiadc") + require.NoError(t, err, "SystemGetLocalCertificate") + require.Equal(t, "/O=Gofortiadc Test", certRes.Subject) // Create local certificate group group := SystemLocalCertificateGroup{ @@ -27,9 +34,17 @@ func TestSystemCertficate(t *testing.T) { } err = client.SystemCreateLocalCertificateGroup(group) - if err != nil { - t.Fatalf("SystemCreateLocalCertificateGroup failed with error: %s", err) - } + require.NoError(t, err, "SystemCreateLocalCertificateGroup") + + defer func() { + // Delete local certificate group + err = client.SystemDeleteLocalCertificateGroup("goforti_group") + require.NoError(t, err, "SystemDeleteLocalCertificateGroup") + }() + + // Get local certificate group + _, err = client.SystemGetLocalCertificateGroup(group.Mkey) + require.NoError(t, err, "SystemGetLocalCertificateGroup") // Create local certificate group member member := SystemLocalCertificateGroupMember{ @@ -38,9 +53,13 @@ func TestSystemCertficate(t *testing.T) { } err = client.SystemCreateLocalCertificateGroupMember("goforti_group", member) - if err != nil { - t.Fatalf("SystemCreateLocalCertificateGroupMember failed with error: %s", err) - } + require.NoError(t, err, "SystemCreateLocalCertificateGroupMember") + + defer func() { + // Delete local certificate group member + err = client.SystemDeleteLocalCertificateGroupMember("goforti_group", "1") + require.NoError(t, err, "SystemDeleteLocalCertificateGroupMember") + }() // Update local certificate group member member = SystemLocalCertificateGroupMember{ @@ -51,25 +70,5 @@ func TestSystemCertficate(t *testing.T) { } err = client.SystemUpdateLocalCertificateGroupMember("goforti_group", "1", member) - if err != nil { - t.Fatalf("SystemUpdateLocalCertificateGroupMember failed with error: %s", err) - } - - // Delete local certificate group member - err = client.SystemDeleteLocalCertificateGroupMember("goforti_group", "1") - if err != nil { - t.Fatalf("SystemDeleteLocalCertificateGroupMember failed with error: %s", err) - } - - // Delete local certificate group - err = client.SystemDeleteLocalCertificateGroup("goforti_group") - if err != nil { - t.Fatalf("SystemDeleteLocalCertificateGroup failed with error: %s", err) - } - - // Delete local certificate - err = client.SystemDeleteLocalCertificate("gofortiadc") - if err != nil { - t.Fatalf("SystemDeleteLocalCertificate failed with error: %s", err) - } + require.NoError(t, err, "SystemUpdateLocalCertificateGroupMember") } diff --git a/system_test.go b/system_test.go index 3981cdf..aeaeefe 100644 --- a/system_test.go +++ b/system_test.go @@ -1,18 +1,19 @@ package gofortiadc import ( + "os" "testing" + + "github.com/stretchr/testify/require" ) func TestSystemGlobal(t *testing.T) { client, err := NewClientHelper() - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + + res, err := client.SystemGlobal() + require.NoError(t, err) - _, err = client.SystemGlobal() - if err != nil { - t.Fatal(err) - } + require.Equal(t, res.Hostname, os.Getenv("GOFORTI_HOSTNAME")) } diff --git a/user_login_test.go b/user_login_test.go index 09ab3b1..12917dd 100644 --- a/user_login_test.go +++ b/user_login_test.go @@ -6,30 +6,50 @@ import ( "net/http/cookiejar" "os" "testing" + + "github.com/stretchr/testify/assert" ) func TestLogin(t *testing.T) { - cookieJar, _ := cookiejar.New(nil) - tr := &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + tests := []struct { + addr string + user string + password string + valid bool + }{ + {"badaddr://feezf", "", "", false}, + {"http://fakehostname.bad", "", "", false}, + {os.Getenv("GOFORTI_ADDRESS"), "baduser", "badpassword", false}, + {os.Getenv("GOFORTI_ADDRESS"), os.Getenv("GOFORTI_USERNAME"), "badpassword", false}, + {os.Getenv("GOFORTI_ADDRESS"), os.Getenv("GOFORTI_USERNAME"), os.Getenv("GOFORTI_PASSWORD"), true}, } - httpClient := &http.Client{ - Jar: cookieJar, - Transport: tr, - } + for _, tc := range tests { + cookieJar, _ := cookiejar.New(nil) - fortiClient := Client{ - Client: httpClient, - Address: os.Getenv("GOFORTI_ADDRESS"), - Username: os.Getenv("GOFORTI_USERNAME"), - Password: os.Getenv("GOFORTI_PASSWORD"), - } + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } - err := fortiClient.Login() - if err != nil { - t.Fatal(err) + httpClient := &http.Client{ + Jar: cookieJar, + Transport: tr, + } + + fortiClient := Client{ + Client: httpClient, + Address: tc.addr, + Username: tc.user, + Password: tc.password, + } + + err := fortiClient.Login() + if tc.valid { + assert.NoError(t, err) + } else { + assert.Error(t, err) + } } }