| /* |
| Copyright 2017 Google Inc. |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| */ |
| |
| package adapter |
| |
| import ( |
| "testing" |
| "time" |
| |
| "github.com/apid/istioApigeeAdapter/adapter/config" |
| "github.com/apid/istioApigeeAdapter/mock" |
| "istio.io/mixer/pkg/adapter" |
| ) |
| |
| func TestAxMissingOrg(t *testing.T) { |
| cfg := config.ReportParams{ |
| CollectionURL: "http://" + mockServer.Address(), |
| Key: mock.ValidPublishKey, |
| Secret: mock.ValidPublishSecret, |
| } |
| |
| rp := newReportBuilder() |
| ce := rp.ValidateConfig(&cfg) |
| if ce == nil { |
| t.Fatalf("Expected invalid config") |
| } |
| } |
| |
| func TestAxWrongSecret(t *testing.T) { |
| cfg := config.ReportParams{ |
| Organization: "foo", |
| Environment: "test", |
| CollectionURL: "http://" + mockServer.Address(), |
| Key: mock.ValidPublishKey, |
| Secret: "NOTTHISONE", |
| } |
| |
| rp := newReportBuilder() |
| ce := rp.ValidateConfig(&cfg) |
| if ce != nil { |
| t.Fatalf("Expected valid config and got %s", ce) |
| } |
| |
| aspect, err := rp.NewAccessLogsAspect(mockEnv, &cfg) |
| if err != nil { |
| t.Fatalf("Error creating aspect: %s", err) |
| } |
| |
| err = aspect.LogAccess( |
| []adapter.LogEntry{ |
| { |
| LogName: "test", |
| Timestamp: time.Now().String(), |
| Severity: adapter.Info, |
| Labels: map[string]interface{}{ |
| "sourceIP": "1.2.3.4", |
| }, |
| }, |
| }, |
| ) |
| if err == nil { |
| t.Fatalf("Expected push error and got none") |
| } |
| } |
| |
| func TestAxValidConfig(t *testing.T) { |
| cfg := config.ReportParams{ |
| Organization: "foo", |
| Environment: "test", |
| CollectionURL: "http://" + mockServer.Address(), |
| Key: mock.ValidPublishKey, |
| Secret: mock.ValidPublishSecret, |
| } |
| |
| rp := newReportBuilder() |
| ce := rp.ValidateConfig(&cfg) |
| if ce != nil { |
| t.Fatalf("Expected valid config and got %s", ce) |
| } |
| |
| aspect, err := rp.NewAccessLogsAspect(mockEnv, &cfg) |
| if err != nil { |
| t.Fatalf("Error creating aspect: %s", err) |
| } |
| |
| err = aspect.LogAccess( |
| []adapter.LogEntry{ |
| { |
| LogName: "test", |
| Timestamp: time.Now().String(), |
| Severity: adapter.Info, |
| Labels: map[string]interface{}{ |
| "sourceIP": "1.2.3.4", |
| }, |
| }, |
| }, |
| ) |
| if err != nil { |
| t.Fatalf("Error pushing analytics: %s", err) |
| } |
| } |