From 3c4187d7dc9df2018d168a7d0321df8899986d45 Mon Sep 17 00:00:00 2001 From: donald1218 Date: Wed, 1 May 2024 06:33:12 +0000 Subject: [PATCH] fix: new app interface --- internal/nas/dispatch.go | 2 +- internal/nas/fuzz_test.go | 5 +- internal/sbi/consumer/consumer.go | 17 ++-- internal/sbi/consumer/nrf_service.go | 2 +- pkg/app/app.go | 19 ++++ pkg/app/mock.go | 129 +++++++++++++++++++++++++++ pkg/service/init.go | 19 ++-- pkg/service/mock.go | 111 +++++++++++++---------- 8 files changed, 236 insertions(+), 68 deletions(-) create mode 100644 pkg/app/app.go create mode 100644 pkg/app/mock.go diff --git a/internal/nas/dispatch.go b/internal/nas/dispatch.go index 198e846c..5818f72c 100644 --- a/internal/nas/dispatch.go +++ b/internal/nas/dispatch.go @@ -14,7 +14,7 @@ import ( func Dispatch(ue *context.AmfUe, accessType models.AccessType, procedureCode int64, msg *nas.Message) error { if msg.GmmMessage == nil { - return errors.New("Gmm Message is nil") + return errors.New("gmm Message is nil") } if msg.GsmMessage != nil { diff --git a/internal/nas/fuzz_test.go b/internal/nas/fuzz_test.go index 22c593a0..1191b859 100644 --- a/internal/nas/fuzz_test.go +++ b/internal/nas/fuzz_test.go @@ -216,9 +216,10 @@ func FuzzHandleNAS2(f *testing.F) { f.Fuzz(func(t *testing.T, d []byte) { ctrl := gomock.NewController(t) - m := service.NewMockApp(ctrl) - service.AMF = m + // m := app.NewMockApp(ctrl) + m := service.NewMockAmfAppInterface(ctrl) c, err := consumer.NewConsumer(m) + service.AMF = m require.NoError(t, err) m.EXPECT(). Consumer(). diff --git a/internal/sbi/consumer/consumer.go b/internal/sbi/consumer/consumer.go index d028ae17..b5860566 100644 --- a/internal/sbi/consumer/consumer.go +++ b/internal/sbi/consumer/consumer.go @@ -1,10 +1,7 @@ package consumer import ( - "context" - - amf_context "github.com/free5gc/amf/internal/context" - "github.com/free5gc/amf/pkg/factory" + "github.com/free5gc/amf/pkg/app" "github.com/free5gc/openapi/Namf_Communication" "github.com/free5gc/openapi/Nausf_UEAuthentication" "github.com/free5gc/openapi/Nnrf_NFDiscovery" @@ -16,14 +13,12 @@ import ( "github.com/free5gc/openapi/Nudm_UEContextManagement" ) -type amf interface { - Config() *factory.Config - Context() *amf_context.AMFContext - CancelContext() context.Context +type ConsumerAmf interface { + app.App } type Consumer struct { - amf + ConsumerAmf // consumer services *namfService @@ -35,9 +30,9 @@ type Consumer struct { *nausfService } -func NewConsumer(amf amf) (*Consumer, error) { +func NewConsumer(amf ConsumerAmf) (*Consumer, error) { c := &Consumer{ - amf: amf, + ConsumerAmf: amf, } c.namfService = &namfService{ diff --git a/internal/sbi/consumer/nrf_service.go b/internal/sbi/consumer/nrf_service.go index 230ac268..45998ac9 100644 --- a/internal/sbi/consumer/nrf_service.go +++ b/internal/sbi/consumer/nrf_service.go @@ -298,7 +298,7 @@ func (s *nnrfService) SendRegisterNFInstance(nrfUri, nfInstanceId string, profil func (s *nnrfService) SendDeregisterNFInstance() (problemDetails *models.ProblemDetails, err error) { logger.ConsumerLog.Infof("[AMF] Send Deregister NFInstance") - amfContext := s.consumer.amf.Context() + amfContext := s.consumer.Context() client := s.getNFManagementClient(amfContext.NrfUri) if client == nil { diff --git a/pkg/app/app.go b/pkg/app/app.go new file mode 100644 index 00000000..51e06b24 --- /dev/null +++ b/pkg/app/app.go @@ -0,0 +1,19 @@ +package app + +import ( + amf_context "github.com/free5gc/amf/internal/context" + "github.com/free5gc/amf/pkg/factory" +) + +type App interface { + SetLogEnable(enable bool) + SetLogLevel(level string) + SetReportCaller(reportCaller bool) + + // tlsKeyLogPath would be remove + Start(tlsKeyLogPath string) + Terminate() + + Context() *amf_context.AMFContext + Config() *factory.Config +} diff --git a/pkg/app/mock.go b/pkg/app/mock.go new file mode 100644 index 00000000..abe29b40 --- /dev/null +++ b/pkg/app/mock.go @@ -0,0 +1,129 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: pkg/app/app.go +// +// Generated by this command: +// +// mockgen -source=pkg/app/app.go -package=app +// + +// Package app is a generated GoMock package. +package app + +import ( + reflect "reflect" + + context "github.com/free5gc/amf/internal/context" + factory "github.com/free5gc/amf/pkg/factory" + gomock "go.uber.org/mock/gomock" +) + +// MockApp is a mock of App interface. +type MockApp struct { + ctrl *gomock.Controller + recorder *MockAppMockRecorder +} + +// MockAppMockRecorder is the mock recorder for MockApp. +type MockAppMockRecorder struct { + mock *MockApp +} + +// NewMockApp creates a new mock instance. +func NewMockApp(ctrl *gomock.Controller) *MockApp { + mock := &MockApp{ctrl: ctrl} + mock.recorder = &MockAppMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockApp) EXPECT() *MockAppMockRecorder { + return m.recorder +} + +// Config mocks base method. +func (m *MockApp) Config() *factory.Config { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Config") + ret0, _ := ret[0].(*factory.Config) + return ret0 +} + +// Config indicates an expected call of Config. +func (mr *MockAppMockRecorder) Config() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Config", reflect.TypeOf((*MockApp)(nil).Config)) +} + +// Context mocks base method. +func (m *MockApp) Context() *context.AMFContext { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Context") + ret0, _ := ret[0].(*context.AMFContext) + return ret0 +} + +// Context indicates an expected call of Context. +func (mr *MockAppMockRecorder) Context() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Context", reflect.TypeOf((*MockApp)(nil).Context)) +} + +// SetLogEnable mocks base method. +func (m *MockApp) SetLogEnable(enable bool) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetLogEnable", enable) +} + +// SetLogEnable indicates an expected call of SetLogEnable. +func (mr *MockAppMockRecorder) SetLogEnable(enable any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLogEnable", reflect.TypeOf((*MockApp)(nil).SetLogEnable), enable) +} + +// SetLogLevel mocks base method. +func (m *MockApp) SetLogLevel(level string) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetLogLevel", level) +} + +// SetLogLevel indicates an expected call of SetLogLevel. +func (mr *MockAppMockRecorder) SetLogLevel(level any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLogLevel", reflect.TypeOf((*MockApp)(nil).SetLogLevel), level) +} + +// SetReportCaller mocks base method. +func (m *MockApp) SetReportCaller(reportCaller bool) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetReportCaller", reportCaller) +} + +// SetReportCaller indicates an expected call of SetReportCaller. +func (mr *MockAppMockRecorder) SetReportCaller(reportCaller any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetReportCaller", reflect.TypeOf((*MockApp)(nil).SetReportCaller), reportCaller) +} + +// Start mocks base method. +func (m *MockApp) Start(tlsKeyLogPath string) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Start", tlsKeyLogPath) +} + +// Start indicates an expected call of Start. +func (mr *MockAppMockRecorder) Start(tlsKeyLogPath any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Start", reflect.TypeOf((*MockApp)(nil).Start), tlsKeyLogPath) +} + +// Terminate mocks base method. +func (m *MockApp) Terminate() { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Terminate") +} + +// Terminate indicates an expected call of Terminate. +func (mr *MockAppMockRecorder) Terminate() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Terminate", reflect.TypeOf((*MockApp)(nil).Terminate)) +} diff --git a/pkg/service/init.go b/pkg/service/init.go index 31ea6b92..a8b4a028 100644 --- a/pkg/service/init.go +++ b/pkg/service/init.go @@ -10,18 +10,23 @@ import ( amf_context "github.com/free5gc/amf/internal/context" "github.com/free5gc/amf/internal/logger" "github.com/free5gc/amf/internal/sbi/consumer" + "github.com/free5gc/amf/pkg/app" "github.com/free5gc/amf/pkg/factory" ) -var _ App = &AmfApp{} - -type App interface { - Context() *amf_context.AMFContext - Config() *factory.Config +type AmfAppInterface interface { + app.App + consumer.ConsumerAmf Consumer() *consumer.Consumer } +var AMF AmfAppInterface + +var _ app.App = &AmfApp{} + type AmfApp struct { + AmfAppInterface + cfg *factory.Config amfCtx *amf_context.AMFContext ctx context.Context @@ -33,9 +38,7 @@ type AmfApp struct { terminate func(*AmfApp) } -var AMF App - -func GetApp() App { +func GetApp() AmfAppInterface { return AMF } diff --git a/pkg/service/mock.go b/pkg/service/mock.go index 1a9635c4..8b48f28b 100644 --- a/pkg/service/mock.go +++ b/pkg/service/mock.go @@ -1,63 +1,48 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: init.go +// Source: pkg/service/init.go // // Generated by this command: // -// mockgen -source=init.go -package=service -destination interface.go +// mockgen -source=pkg/service/init.go -package=service // // Package service is a generated GoMock package. package service import ( - context "context" reflect "reflect" - context0 "github.com/free5gc/amf/internal/context" + context "github.com/free5gc/amf/internal/context" consumer "github.com/free5gc/amf/internal/sbi/consumer" factory "github.com/free5gc/amf/pkg/factory" gomock "go.uber.org/mock/gomock" ) -// MockApp is a mock of App interface. -type MockApp struct { +// MockAmfAppInterface is a mock of AmfAppInterface interface. +type MockAmfAppInterface struct { ctrl *gomock.Controller - recorder *MockAppMockRecorder + recorder *MockAmfAppInterfaceMockRecorder } -// MockAppMockRecorder is the mock recorder for MockApp. -type MockAppMockRecorder struct { - mock *MockApp +// MockAmfAppInterfaceMockRecorder is the mock recorder for MockAmfAppInterface. +type MockAmfAppInterfaceMockRecorder struct { + mock *MockAmfAppInterface } -// NewMockApp creates a new mock instance. -func NewMockApp(ctrl *gomock.Controller) *MockApp { - mock := &MockApp{ctrl: ctrl} - mock.recorder = &MockAppMockRecorder{mock} +// NewMockAmfAppInterface creates a new mock instance. +func NewMockAmfAppInterface(ctrl *gomock.Controller) *MockAmfAppInterface { + mock := &MockAmfAppInterface{ctrl: ctrl} + mock.recorder = &MockAmfAppInterfaceMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockApp) EXPECT() *MockAppMockRecorder { +func (m *MockAmfAppInterface) EXPECT() *MockAmfAppInterfaceMockRecorder { return m.recorder } -// CancelContext mocks base method. -func (m *MockApp) CancelContext() context.Context { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CancelContext") - ret0, _ := ret[0].(context.Context) - return ret0 -} - -// CancelContext indicates an expected call of CancelContext. -func (mr *MockAppMockRecorder) CancelContext() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CancelContext", reflect.TypeOf((*MockApp)(nil).CancelContext)) -} - // Config mocks base method. -func (m *MockApp) Config() *factory.Config { +func (m *MockAmfAppInterface) Config() *factory.Config { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Config") ret0, _ := ret[0].(*factory.Config) @@ -65,13 +50,13 @@ func (m *MockApp) Config() *factory.Config { } // Config indicates an expected call of Config. -func (mr *MockAppMockRecorder) Config() *gomock.Call { +func (mr *MockAmfAppInterfaceMockRecorder) Config() *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Config", reflect.TypeOf((*MockApp)(nil).Config)) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Config", reflect.TypeOf((*MockAmfAppInterface)(nil).Config)) } // Consumer mocks base method. -func (m *MockApp) Consumer() *consumer.Consumer { +func (m *MockAmfAppInterface) Consumer() *consumer.Consumer { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Consumer") ret0, _ := ret[0].(*consumer.Consumer) @@ -79,45 +64,81 @@ func (m *MockApp) Consumer() *consumer.Consumer { } // Consumer indicates an expected call of Consumer. -func (mr *MockAppMockRecorder) Consumer() *gomock.Call { +func (mr *MockAmfAppInterfaceMockRecorder) Consumer() *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Consumer", reflect.TypeOf((*MockApp)(nil).Consumer)) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Consumer", reflect.TypeOf((*MockAmfAppInterface)(nil).Consumer)) } // Context mocks base method. -func (m *MockApp) Context() *context0.AMFContext { +func (m *MockAmfAppInterface) Context() *context.AMFContext { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Context") - ret0, _ := ret[0].(*context0.AMFContext) + ret0, _ := ret[0].(*context.AMFContext) return ret0 } // Context indicates an expected call of Context. -func (mr *MockAppMockRecorder) Context() *gomock.Call { +func (mr *MockAmfAppInterfaceMockRecorder) Context() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Context", reflect.TypeOf((*MockAmfAppInterface)(nil).Context)) +} + +// SetLogEnable mocks base method. +func (m *MockAmfAppInterface) SetLogEnable(enable bool) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetLogEnable", enable) +} + +// SetLogEnable indicates an expected call of SetLogEnable. +func (mr *MockAmfAppInterfaceMockRecorder) SetLogEnable(enable any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLogEnable", reflect.TypeOf((*MockAmfAppInterface)(nil).SetLogEnable), enable) +} + +// SetLogLevel mocks base method. +func (m *MockAmfAppInterface) SetLogLevel(level string) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetLogLevel", level) +} + +// SetLogLevel indicates an expected call of SetLogLevel. +func (mr *MockAmfAppInterfaceMockRecorder) SetLogLevel(level any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLogLevel", reflect.TypeOf((*MockAmfAppInterface)(nil).SetLogLevel), level) +} + +// SetReportCaller mocks base method. +func (m *MockAmfAppInterface) SetReportCaller(reportCaller bool) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetReportCaller", reportCaller) +} + +// SetReportCaller indicates an expected call of SetReportCaller. +func (mr *MockAmfAppInterfaceMockRecorder) SetReportCaller(reportCaller any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Context", reflect.TypeOf((*MockApp)(nil).Context)) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetReportCaller", reflect.TypeOf((*MockAmfAppInterface)(nil).SetReportCaller), reportCaller) } // Start mocks base method. -func (m *MockApp) Start(tlsKeyLogPath string) { +func (m *MockAmfAppInterface) Start(tlsKeyLogPath string) { m.ctrl.T.Helper() m.ctrl.Call(m, "Start", tlsKeyLogPath) } // Start indicates an expected call of Start. -func (mr *MockAppMockRecorder) Start(tlsKeyLogPath any) *gomock.Call { +func (mr *MockAmfAppInterfaceMockRecorder) Start(tlsKeyLogPath any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Start", reflect.TypeOf((*MockApp)(nil).Start), tlsKeyLogPath) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Start", reflect.TypeOf((*MockAmfAppInterface)(nil).Start), tlsKeyLogPath) } // Terminate mocks base method. -func (m *MockApp) Terminate() { +func (m *MockAmfAppInterface) Terminate() { m.ctrl.T.Helper() m.ctrl.Call(m, "Terminate") } // Terminate indicates an expected call of Terminate. -func (mr *MockAppMockRecorder) Terminate() *gomock.Call { +func (mr *MockAmfAppInterfaceMockRecorder) Terminate() *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Terminate", reflect.TypeOf((*MockApp)(nil).Terminate)) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Terminate", reflect.TypeOf((*MockAmfAppInterface)(nil).Terminate)) }