Increase coverage (#2551)

Signed-off-by: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com>
This commit is contained in:
Daniel Valdivia
2023-01-05 14:36:21 -08:00
committed by GitHub
parent 287af260b7
commit 1cb2fca7a5
9 changed files with 800 additions and 2 deletions

View File

@@ -73,6 +73,5 @@ func getMarketplace() string {
// Get DirectPVMode
func getDirectPVEnabled() bool {
currentMode := env.Get(DirectPVMode, "off")
return currentMode == "on"
}

View File

@@ -19,6 +19,8 @@ package operatorapi
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_getK8sSAToken(t *testing.T) {
@@ -96,3 +98,39 @@ func Test_getMarketplace(t *testing.T) {
})
}
}
func Test_getDirectPVEnabled(t *testing.T) {
type args struct {
setEnv bool
}
tests := []struct {
name string
want bool
args args
}{
{
name: "DirectPV Mode is Set",
want: true,
args: args{
setEnv: true,
},
},
{
name: "DirectPV Mode is not set",
want: false,
args: args{
setEnv: false,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.args.setEnv {
os.Setenv(DirectPVMode, "on")
} else {
os.Unsetenv(DirectPVMode)
}
assert.Equalf(t, tt.want, getDirectPVEnabled(), "getDirectPVEnabled()")
})
}
}

110
operatorapi/logs_test.go Normal file
View File

@@ -0,0 +1,110 @@
// This file is part of MinIO Console Server
// Copyright (c) 2022 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package operatorapi
import (
"flag"
"fmt"
"testing"
"github.com/minio/cli"
"github.com/stretchr/testify/assert"
)
func TestContext_Load(t *testing.T) {
type fields struct {
Host string
HTTPPort int
HTTPSPort int
TLSRedirect string
TLSCertificate string
TLSKey string
TLSca string
}
type args struct {
values map[string]string
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "valid args",
args: args{
values: map[string]string{
"tls-redirect": "on",
},
},
wantErr: false,
},
{
name: "invalid args",
args: args{
values: map[string]string{
"tls-redirect": "aaaa",
},
},
wantErr: true,
},
{
name: "invalid port http",
args: args{
values: map[string]string{
"tls-redirect": "on",
"port": "65536",
},
},
wantErr: true,
},
{
name: "invalid port https",
args: args{
values: map[string]string{
"tls-redirect": "on",
"port": "65534",
"tls-port": "65536",
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Context{}
fs := flag.NewFlagSet("flags", flag.ContinueOnError)
for k, v := range tt.args.values {
fs.String(k, v, "ok")
}
ctx := cli.NewContext(nil, fs, &cli.Context{})
err := c.Load(ctx)
if tt.wantErr {
assert.NotNilf(t, err, fmt.Sprintf("Load(%v)", err))
} else {
assert.Nilf(t, err, fmt.Sprintf("Load(%v)", err))
}
})
}
}
func Test_logInfo(t *testing.T) {
logInfo("message", nil)
}

View File

@@ -18,9 +18,13 @@ package operatorapi
import (
"encoding/json"
"net/http"
"reflect"
"testing"
"github.com/minio/console/operatorapi/operations/operator_api"
"github.com/stretchr/testify/assert"
"github.com/minio/console/models"
)
@@ -77,3 +81,51 @@ func Test_getParityInfo(t *testing.T) {
})
}
}
func Test_getParityResponse(t *testing.T) {
type args struct {
params operator_api.GetParityParams
}
tests := []struct {
name string
args args
want models.ParityResponse
wantErr bool
}{
{
name: "valid",
args: args{
params: operator_api.GetParityParams{
HTTPRequest: &http.Request{},
DisksPerNode: 4,
Nodes: 4,
},
},
want: models.ParityResponse{"EC:8", "EC:7", "EC:6", "EC:5", "EC:4", "EC:3", "EC:2"},
wantErr: false,
},
{
name: "invalid",
args: args{
params: operator_api.GetParityParams{
HTTPRequest: &http.Request{},
DisksPerNode: -4,
Nodes: 4,
},
},
want: models.ParityResponse(nil),
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := getParityResponse(tt.args.params)
assert.Equalf(t, tt.want, got, "getParityResponse(%v)", tt.args.params)
if tt.wantErr {
assert.NotNilf(t, got1, "getParityResponse(%v)", tt.args.params)
} else {
assert.Nilf(t, got1, "getParityResponse(%v)", tt.args.params)
}
})
}
}

View File

@@ -228,3 +228,27 @@ func TestGenerateTenantConfigurationFile(t *testing.T) {
})
}
}
func Test_stringPtr(t *testing.T) {
type args struct {
str string
}
tests := []struct {
name string
args args
wantNil bool
}{
{
name: "get a pointer",
args: args{
str: "",
},
wantNil: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.NotNilf(t, stringPtr(tt.args.str), "stringPtr(%v)", tt.args.str)
})
}
}