Skip to content

Commit

Permalink
Merge pull request #1 from hatamiarash7/unit-test
Browse files Browse the repository at this point in the history
Unit test
  • Loading branch information
hatamiarash7 authored Mar 31, 2023
2 parents 4a56af1 + 5841f99 commit a447f8b
Show file tree
Hide file tree
Showing 9 changed files with 715 additions and 27 deletions.
34 changes: 26 additions & 8 deletions .github/workflows/codeql.yaml
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
name: "CodeQL"
name: CodeQL

on:
push:
branches: ["main"]
branches:
- main
paths:
- cmd
- pkg
- go.mod
- go.sum
- .github/workflows/codeql.yaml
pull_request:
branches: ["main"]
schedule:
- cron: "42 5 * * 1"
branches:
- main
paths:
- cmd
- pkg
- go.mod
- go.sum
- .github/workflows/codeql.yaml
types: [opened, reopened, synchronize, ready_for_review]
workflow_dispatch:

jobs:
analyze:
name: Analyze
if: github.event_name != 'pull_request' || !github.event.pull_request.draft
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

steps:
- name: Checkout repository
- name: Checkout
uses: actions/checkout@v3

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: "go"

- name: Build code
- name: Build
run: make cli

- name: Perform CodeQL Analysis
- name: Test
run: go test -v -race ./...

- name: CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:go"
19 changes: 14 additions & 5 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,23 @@ jobs:
- name: Install Go dependencies
run: go mod download

- name: Build application
- name: Build
env:
GOOS: ${{ matrix.os }}
GOARCH: ${{ matrix.arch }}
CGO_ENABLED: 0
LDFLAGS: -s -w -X github.com/arvancloud/cdn-go/internal/pkg/version.version=${{ steps.slug.outputs.version }}
run: go build -trimpath -ldflags "$LDFLAGS" -o "./${{ steps.values.outputs.binary-name }}" cmd/*.go

- name: Test
run: go test -v -race ./... -json > TestResults.json

- name: Upload test results
uses: actions/upload-artifact@v3
with:
name: Test-results
path: TestResults.json

- name: Run UPX
uses: crazy-max/ghaction-upx@v2
with:
Expand All @@ -63,7 +72,7 @@ jobs:
./${{ steps.values.outputs.binary-name }}
args: --best --lzma

- name: Upload binaries to release
- name: Upload binaries
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ github.token }}
Expand Down Expand Up @@ -99,7 +108,7 @@ jobs:
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"

- name: Build & Push Docker image
- name: Build & Push image
uses: docker/build-push-action@v4
with:
context: .
Expand All @@ -113,7 +122,7 @@ jobs:
r1cloud/cdn:${{ steps.slug.outputs.version }}
r1cloud/cdn:latest
- name: Run Trivy vulnerability scanner
- name: Run Trivy
uses: aquasecurity/trivy-action@master
with:
image-ref: r1cloud/cdn:latest
Expand All @@ -125,7 +134,7 @@ jobs:
template: "@/contrib/sarif.tpl"
output: "trivy-results.sarif"

- name: Upload Trivy scan results to GitHub Security tab
- name: Upload Trivy scan results
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: "trivy-results.sarif"
12 changes: 11 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{
"cSpell.words": ["ANAME", "gcli", "gookit", "iodef", "issuewild", "TLSA"]
"cSpell.words": [
"ANAME",
"backoff",
"gcli",
"gookit",
"iodef",
"issuewild",
"stretchr",
"TLSA",
"unmarshalling"
]
}
73 changes: 73 additions & 0 deletions pkg/arvancloud_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package arvancloud

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

var (
// HTTP request multiplexer
mux *http.ServeMux

// API client for test
client *API

// HTTP server used for mocking
server *httptest.Server
)

func setup(opts ...Option) {
// Create a test server
mux = http.NewServeMux()
server = httptest.NewServer(mux)

// Disable rate limits and retries for test purpose
opts = append([]Option{UsingRateLimit(100000), UsingRetryPolicy(0, 0, 0)}, opts...)

// Configure client
client, _ = New("deadbeef", opts...)
client.BaseURL = server.URL
}

func teardown() {
server.Close()
}

func TestHeaders(t *testing.T) {
// Default
setup()
mux.HandleFunc("/domains/"+testDomain+"/dns-records", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
})
teardown()

// Override defaults
headers := make(http.Header)
headers.Set("Content-Type", "application/graphql")
headers.Add("H1", "V1")
headers.Add("H2", "V2")
setup(Headers(headers))
mux.HandleFunc("/domains/"+testDomain+"/dns-records", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
assert.Equal(t, "application/graphql", r.Header.Get("Content-Type"))
assert.Equal(t, "V1", r.Header.Get("H1"))
assert.Equal(t, "V2", r.Header.Get("H2"))
})
teardown()

// Authentication
setup()
client, err := New("TEST_TOKEN")
assert.NoError(t, err)
client.BaseURL = server.URL
mux.HandleFunc("/domains/"+testDomain+"/dns-records", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
assert.Equal(t, "Bearer TEST_TOKEN", r.Header.Get("Authorization"))
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
})
teardown()
}
6 changes: 6 additions & 0 deletions pkg/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package arvancloud

const (
// Testing
testDomain = "test.ir"
)
24 changes: 11 additions & 13 deletions pkg/dns_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ package arvancloud

// DSNRecord is a DSN record structure for a domain
type DNSRecord struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Value interface{} `json:"value,omitempty"`
TTL int `json:"ttl,omitempty"`
Cloud bool `json:"cloud,omitempty"`
UpstreamHTTPS string `json:"upstream_https,omitempty"`
IPFilterMode interface{} `json:"ip_filter_mode,omitempty"`
IsProtected bool `json:"is_protected,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
MonitoringStatus string `json:"monitoring_status,omitempty"`
HealthCheck interface{} `json:"health_check,omitempty"`
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Value interface{} `json:"value,omitempty"`
TTL int `json:"ttl,omitempty"`
Cloud bool `json:"cloud,omitempty"`
UpstreamHTTPS string `json:"upstream_https,omitempty"`
IPFilterMode interface{} `json:"ip_filter_mode,omitempty"`
IsProtected bool `json:"is_protected,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}

// DNSRecord_Response is response structure contains
Expand Down
Loading

0 comments on commit a447f8b

Please sign in to comment.