-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from logzio/add-tests
Add tests & comments
- Loading branch information
Showing
21 changed files
with
2,001 additions
and
821 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,4 @@ RUN go build -o main . | |
|
||
FROM alpine:3.14 | ||
COPY --from=build /app/main /app/main | ||
|
||
CMD ["/app/main"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package common | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/runtime" | ||
fakeDynamic "k8s.io/client-go/dynamic/fake" | ||
"k8s.io/client-go/kubernetes/fake" | ||
"testing" | ||
) | ||
|
||
func CreateFakeClient() (mockClient *fake.Clientset) { | ||
|
||
mockClient = fake.NewSimpleClientset() | ||
|
||
return mockClient | ||
} | ||
func CreateDynamicFakeClient() (mockDynamicClient *fakeDynamic.FakeDynamicClient) { | ||
scheme := runtime.NewScheme() | ||
|
||
mockDynamicClient = fakeDynamic.NewSimpleDynamicClient(scheme) | ||
|
||
return mockDynamicClient | ||
} | ||
|
||
// TestFakeClient demonstrates how to use a fake client with SharedInformerFactory in tests. | ||
func TestFakeDynamicClient(t *testing.T) { | ||
// Create the fake client. | ||
fakeDynamicClient := CreateDynamicFakeClient() | ||
if fakeDynamicClient == nil { | ||
t.Error("Failed to create fake dynamic client") | ||
} else { | ||
t.Log("Created fake dynamic client") | ||
} | ||
|
||
} | ||
func TestFakeClusterClient(t *testing.T) { | ||
// Create the fake client. | ||
fakeK8sClient := CreateFakeClient() | ||
|
||
if fakeK8sClient == nil { | ||
t.Error("Failed to create fake client") | ||
} else { | ||
t.Log("Created fake client") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package common | ||
|
||
import ( | ||
"github.com/logzio/logzio-go" | ||
"main.go/mockLogzioListener" | ||
"testing" | ||
|
||
"os" | ||
"time" | ||
) | ||
|
||
func TestStartMockLogzioListener(t *testing.T) { | ||
mockLogzioListener.StartMockLogzioListener() | ||
mockListener := mockLogzioListener.MockListener | ||
if mockListener != nil { | ||
mockListenerURL := mockLogzioListener.GetMockListenerURL() | ||
logsCount := mockListener.NumberOfLogs() | ||
if mockListenerURL != "" { | ||
err := os.Setenv("LOGZIO_TOKEN", "test-shipping-token") | ||
if err != nil { | ||
return | ||
} | ||
err = os.Setenv("LOGZIO_LISTENER", mockListenerURL) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if logsCount > 0 { | ||
t.Log("Successfully sent logs.") | ||
} | ||
} | ||
} else { | ||
t.Error("Failed to start mock listener") | ||
} | ||
|
||
} | ||
|
||
func TestConfigureLogzioLogger(t *testing.T) { | ||
// Creates a resources using Logz.io output configuration: https://app.logz.io/#/dashboard/send-your-data/log-sources/go | ||
var err error | ||
LogzioToken := os.Getenv("LOGZIO_TOKEN") // Log shipping token for Logz.io | ||
if LogzioToken != "" { | ||
LogzioListener := os.Getenv("LOGZIO_LISTENER") | ||
if LogzioListener == "" { | ||
LogzioListener = "https://listener.logz.io:8071" // Defaults to us-east-1 region | ||
} | ||
LogzioLogger, err = logzio.New( | ||
LogzioToken, | ||
logzio.SetDebug(os.Stderr), | ||
logzio.SetUrl(LogzioListener), | ||
logzio.SetDrainDuration(time.Second*5), | ||
logzio.SetTempDirectory("myQueue"), | ||
logzio.SetDrainDiskThreshold(99), | ||
) | ||
if err != nil { | ||
t.Errorf("\n[FATAL] Failed to configure the Logz.io logger.\nERROR: %v\n", err) | ||
} else { | ||
t.Log("Successfully configured the Logz.io logger.\n") | ||
} | ||
} else { | ||
t.Error("\n[FATAL] Invalid token configured for LOGZIO_TOKEN environment variable.\n") | ||
} | ||
|
||
} | ||
func TestSendLog(t *testing.T) { | ||
|
||
t.Run("SendLog", func(t *testing.T) { | ||
os.Setenv("ENV_ID", "dev") | ||
os.Setenv("LOG_TYPE", "logzio-k8s-events-test") | ||
logsListInstance := mockLogzioListener.GetLogsListInstance() | ||
allLogs := logsListInstance.List | ||
for _, testLog := range allLogs { | ||
SendLog("Test log", testLog) | ||
} | ||
}) | ||
} |
Oops, something went wrong.