Skip to content

Commit

Permalink
Merge pull request #5 from mohsenmottaghi/fix-ci-tests
Browse files Browse the repository at this point in the history
fix(tests): issue in CI without MMDB files
  • Loading branch information
mohsenmottaghi authored Oct 11, 2024
2 parents 7a3069c + cf0ea2e commit 8eca0fd
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 18 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

const (
version = "v0.4.0"
version = "v0.4.1"
maintainer = "The InfraZ Authors"
)

Expand Down
31 changes: 31 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
13 changes: 9 additions & 4 deletions pkg/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package inspect

import (
"encoding/json"
"errors"
"log"
"net"
"strings"
Expand All @@ -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, "/") {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 20 additions & 12 deletions pkg/inspect/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package inspect
import (
"encoding/json"
"net"
"os"
"testing"
)

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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")
}
}
10 changes: 9 additions & 1 deletion pkg/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 8eca0fd

Please sign in to comment.