NVIDIA GPU loadable plugin
Source
nvmlmock.NewExpectation("GetDeviceCountV2").ProvideOutput(uint(1)).ProvideError(errors.New("fail")),
/*
** Copyright 2001-2024 Zabbix SIA
**
** 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 handlers
import (
"context"
"errors"
"sync"
"testing"
"github.com/google/go-cmp/cmp"
"golang.zabbix.com/plugin/nvidia/internal/plugin/params"
"golang.zabbix.com/plugin/nvidia/pkg/nvml"
nvmlmock "golang.zabbix.com/plugin/nvidia/pkg/nvml-mock"
"golang.zabbix.com/sdk/errs"
)
func TestHandler_GetNVMLVersion(t *testing.T) {
t.Parallel()
type expect struct {
expectations []*nvmlmock.Expectation
}
tests := []struct {
name string
expect expect
want any
wantErr bool
}{
{
"+valid",
expect{
expectations: []*nvmlmock.Expectation{
nvmlmock.NewExpectation("GetNVMLVersion").ProvideOutput("Mock NVML Version"),
},
},
"Mock NVML Version",
false,
},
{
"-nvmlRunnerGetNVMLVersionError",
expect{
expectations: []*nvmlmock.Expectation{
nvmlmock.NewExpectation("GetNVMLVersion").ProvideOutput("").ProvideError(errors.New("fail")),
},
},
"",
true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
runner := nvmlmock.NewMockRunner(t).ExpectCalls(tt.expect.expectations...)
// Initialize Handler with the mocked nvmlRunner
h := &Handler{
nvmlRunner: runner,
}
// Call the method being tested
got, err := h.GetNVMLVersion(context.Background(), nil, nil...)
// Check for error match
if (err != nil) != tt.wantErr {
t.Fatalf("Handler.DriverVersion() error = %v, wantErr %v", err, tt.wantErr)
}
// Check for result match
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Fatalf("Handler.DriverVersion() = %s", diff)
}
if !runner.ExpectedCallsDone() {