From cf0ea2e39a3c2a1c0034f7b76ab5c2cfc7f01494 Mon Sep 17 00:00:00 2001 From: Mohsen Mottaghi Date: Sat, 12 Oct 2024 02:07:28 +0330 Subject: [PATCH] fix(tests): issue in CI without MMDB files Signed-off-by: Mohsen Mottaghi --- CHANGELOG.md | 7 +++++++ cmd/version.go | 2 +- main_test.go | 31 +++++++++++++++++++++++++++++++ pkg/inspect/inspect.go | 13 +++++++++---- pkg/inspect/inspect_test.go | 32 ++++++++++++++++++++------------ pkg/metadata/metadata_test.go | 10 +++++++++- 6 files changed, 77 insertions(+), 18 deletions(-) create mode 100644 main_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index bd1cff9..0e565e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 0.4.1 +### Fixed +- Fix tests issue in CI without MMDB files + +### Test +- Add test for main.go + ## 0.4.0 ### Added - Add CI for testing and building the project diff --git a/cmd/version.go b/cmd/version.go index e88aa32..aa86f47 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -23,7 +23,7 @@ import ( ) const ( - version = "v0.4.0" + version = "v0.4.1" maintainer = "The InfraZ Authors" ) diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..43587b2 --- /dev/null +++ b/main_test.go @@ -0,0 +1,31 @@ +/* +Copyright 2024 The InfraZ Authors. + +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 main + +import ( + "testing" +) + +func TestMain(t *testing.T) { + defer func() { + if r := recover(); r != nil { + t.Errorf("main() panicked with error: %v", r) + } + }() + + main() +} diff --git a/pkg/inspect/inspect.go b/pkg/inspect/inspect.go index c2b8d6a..2f05519 100644 --- a/pkg/inspect/inspect.go +++ b/pkg/inspect/inspect.go @@ -18,6 +18,7 @@ package inspect import ( "encoding/json" + "errors" "log" "net" "strings" @@ -30,7 +31,7 @@ type CmdInspectConfig struct { Inputs []string } -func determineLookupNetwork(input string) string { +func determineLookupNetwork(input string) (string, error) { var lookupNetwork string if !strings.Contains(input, "/") { @@ -39,13 +40,14 @@ func determineLookupNetwork(input string) string { } else if strings.Contains(input, ":") { lookupNetwork = input + "/128" } else { - log.Fatal("Invalid input format") + err := errors.New("Invalid input") + return lookupNetwork, err } } else { lookupNetwork = input } - return lookupNetwork + return lookupNetwork, nil } func mmdbReader(input string) (*maxminddb.Reader, error) { @@ -87,7 +89,10 @@ func InspectInMMDB(cfg CmdInspectConfig) ([]byte, error) { }) // Determine the lookup network - lookupNetwork := determineLookupNetwork(input) + lookupNetwork, err := determineLookupNetwork(input) + if err != nil { + log.Fatalf("[!] Invalid input: %s", input) + } // Check if lookupNetwork is valid CIDR _, netIPNet, err := net.ParseCIDR(lookupNetwork) diff --git a/pkg/inspect/inspect_test.go b/pkg/inspect/inspect_test.go index e7c92a1..6cc030b 100644 --- a/pkg/inspect/inspect_test.go +++ b/pkg/inspect/inspect_test.go @@ -19,6 +19,7 @@ package inspect import ( "encoding/json" "net" + "os" "testing" ) @@ -34,24 +35,20 @@ func TestDetermineLookupNetwork(t *testing.T) { } for _, test := range tests { - result := determineLookupNetwork(test.input) - if result != test.expected { + result, err := determineLookupNetwork(test.input) + if err != nil { + t.Errorf("determineLookupNetwork(%s) returned error: %v", test.input, err) + } else if result != test.expected { t.Errorf("determineLookupNetwork(%s) = %s; expected %s", test.input, result, test.expected) } } } -func TestDetermineLookupNetworkInvalidInput(t *testing.T) { - defer func() { - if r := recover(); r == nil { - t.Errorf("determineLookupNetwork did not panic on invalid input") - } - }() - - determineLookupNetwork("invalid_input") -} - func TestMMDBReader(t *testing.T) { + // Skip test if running on GitHub Actions as the test requires the GeoLite2-Country.mmdb file + if os.Getenv("CI") == "true" { + t.Skip("Skipping test; running on GitHub Actions") + } testInput := "../../output/GeoLite2-Country.mmdb" _, err := mmdbReader(testInput) @@ -61,6 +58,10 @@ func TestMMDBReader(t *testing.T) { } func TestMMDBLookup(t *testing.T) { + // Skip test if running on GitHub Actions as the test requires the GeoLite2-Country.mmdb file + if os.Getenv("CI") == "true" { + t.Skip("Skipping test; running on GitHub Actions") + } testInput := "../../output/GeoLite2-Country.mmdb" testsData := []struct { query string @@ -84,3 +85,10 @@ func TestMMDBLookup(t *testing.T) { } } } + +func TestDetermineLookupNetworkInvalidInput(t *testing.T) { + _, err := determineLookupNetwork("invalid_input") + if err == nil { + t.Errorf("determineLookupNetwork did not return an error on invalid input") + } +} diff --git a/pkg/metadata/metadata_test.go b/pkg/metadata/metadata_test.go index 23eb1c1..5f6ad2f 100644 --- a/pkg/metadata/metadata_test.go +++ b/pkg/metadata/metadata_test.go @@ -16,9 +16,17 @@ limitations under the License. package metadata -import "testing" +import ( + "os" + "testing" +) func TestMetadataMMDB(t *testing.T) { + // Skip test if running on GitHub Actions as the test requires the GeoLite2-Country.mmdb file + if os.Getenv("CI") == "true" { + t.Skip("Skipping test; running on GitHub Actions") + } + expected := `{"description":{"en":"GeoLite2 Country database"},"database_type":"GeoLite2-Country","languages":["de","en","es","fr","ja","pt-BR","ru","zh-CN"],"binary_format_major_version":2,"binary_format_minor_version":0,"build_epoch":1726249063,"ip_version":6,"node_count":1234259,"record_size":24}` testMMDBFile := "../../output/GeoLite2-Country.mmdb"