Skip to content

Commit

Permalink
fix golint
Browse files Browse the repository at this point in the history
  • Loading branch information
lminzhw committed Jul 3, 2019
1 parent 4c674f9 commit e93d105
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 89 deletions.
29 changes: 24 additions & 5 deletions pkg/cli/job/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,33 @@ func TestListJob(t *testing.T) {
server := httptest.NewServer(handler)
defer server.Close()

listJobFlags.Master = server.URL
listJobFlags.Namespace = "test"

testCases := []struct {
Name string
ExpectValue error
Name string
ExpectValue error
AllNamespace bool
Selector string
}{
{
Name: "ListJob",
ExpectValue: nil,
},
{
Name: "ListAllNamespaceJob",
ExpectValue: nil,
AllNamespace: true,
},
}

for i, testcase := range testCases {
listJobFlags = &listFlags{
commonFlags: commonFlags{
Master: server.URL,
},
Namespace: "test",
allNamespace: testcase.AllNamespace,
selector: testcase.Selector,
}

err := ListJobs()
if err != nil {
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err)
Expand All @@ -74,5 +87,11 @@ func TestInitListFlags(t *testing.T) {
if cmd.Flag("scheduler") == nil {
t.Errorf("Could not find the flag scheduler")
}
if cmd.Flag("all-namespaces") == nil {
t.Errorf("Could not find the flag all-namespaces")
}
if cmd.Flag("selector") == nil {
t.Errorf("Could not find the flag selector")
}

}
4 changes: 2 additions & 2 deletions pkg/cli/job/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ func readFile(filename string) (*vkapi.Job, error) {
}

if !strings.Contains(filename, ".yaml") && !strings.Contains(filename, ".yml") {
return nil, fmt.Errorf("Only support yaml file.")
return nil, fmt.Errorf("only support yaml file")
}

file, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("Failed to read file, err: %v", err)
return nil, fmt.Errorf("failed to read file, err: %v", err)
}

var job vkapi.Job
Expand Down
30 changes: 27 additions & 3 deletions pkg/cli/job/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ package job
import (
"encoding/json"
"github.com/spf13/cobra"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

v1alpha1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
)
Expand All @@ -41,21 +44,42 @@ func TestCreateJob(t *testing.T) {
server := httptest.NewServer(handler)
defer server.Close()

launchJobFlags.Master = server.URL
launchJobFlags.Namespace = "test"
launchJobFlags.Requests = "cpu=1000m,memory=100Mi"
fileName := time.Now().String() + "testCreateJob.yaml"
val, err := json.Marshal(response)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(fileName, val, os.ModePerm)
if err != nil {
panic(err)
}
defer os.Remove(fileName)

testCases := []struct {
Name string
ExpectValue error
FileName string
}{
{
Name: "CreateJob",
ExpectValue: nil,
},
{
Name: "CreateJobWithFile",
FileName: fileName,
ExpectValue: nil,
},
}

for i, testcase := range testCases {
launchJobFlags = &runFlags{
commonFlags: commonFlags{
Master: server.URL,
},
Namespace: "test",
Requests: "cpu=1000m,memory=100Mi",
}

err := RunJob()
if err != nil {
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err)
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/job/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func translateTimestampSince(timestamp metav1.Time) string {
return HumanDuration(time.Since(timestamp.Time))
}

// HumanDuration translate time.Duration to human readable time string
func HumanDuration(d time.Duration) string {
// Allow deviation no more than 2 seconds(excluded) to tolerate machine time
// inconsistence, it can be considered as almost now.
Expand Down
109 changes: 109 additions & 0 deletions pkg/cli/job/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
Copyright 2019 The Volcano 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 job

import (
"testing"

"time"
)

func TestJobUtil(t *testing.T) {
testCases := []struct {
Name string
Duration time.Duration
ExpectValue string
}{
{
Name: "InvalidTime",
Duration: -time.Minute,
ExpectValue: "<invalid>",
},
{
Name: "SmallInvalieTime",
Duration: -time.Millisecond,
ExpectValue: "0s",
},
{
Name: "NormalSeconds",
Duration: 62 * time.Second,
ExpectValue: "62s",
},
{
Name: "NormalMinutes",
Duration: 180 * time.Second,
ExpectValue: "3m",
},
{
Name: "NormalMinutesWithSecond",
Duration: 190 * time.Second,
ExpectValue: "3m10s",
},
{
Name: "BiggerMinutesWithoutSecond",
Duration: 121*time.Minute + 56*time.Second,
ExpectValue: "121m",
},
{
Name: "NormalHours",
Duration: 5*time.Hour + 9*time.Second,
ExpectValue: "5h",
},
{
Name: "NormalHoursWithMinute",
Duration: 5*time.Hour + 7*time.Minute + 9*time.Second,
ExpectValue: "5h7m",
},
{
Name: "BiggerHoursWithoutMinute",
Duration: 12*time.Hour + 7*time.Minute + 9*time.Second,
ExpectValue: "12h",
},
{
Name: "NormalDays",
Duration: 5*24*time.Hour + 7*time.Minute + 9*time.Second,
ExpectValue: "5d",
},
{
Name: "NormalDaysWithHours",
Duration: 5*24*time.Hour + 9*time.Hour,
ExpectValue: "5d9h",
},
{
Name: "BiggerDayWithoutHours",
Duration: 531*24*time.Hour + 7*time.Minute + 9*time.Second,
ExpectValue: "531d",
},
{
Name: "NormalYears",
Duration: (365*5+89)*24*time.Hour + 7*time.Minute + 9*time.Second,
ExpectValue: "5y89d",
},
{
Name: "BiggerYears",
Duration: (365*12+15)*24*time.Hour + 7*time.Minute + 9*time.Second,
ExpectValue: "12y",
},
}

for i, testcase := range testCases {
answer := HumanDuration(testcase.Duration)
if answer != testcase.ExpectValue {
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, answer)
}
}
}
Loading

0 comments on commit e93d105

Please sign in to comment.